kiwicopple / doctest-js

Run JSDoc style doc examples as tests within your test suite
MIT License
101 stars 4 forks source link

`doctest is not a function` #108

Open loganpowell opened 3 years ago

loganpowell commented 3 years ago

Bug report

TypeError: doctest is not a function

Describe the bug

Following the README, I used this example:

// src/sum.js

/**
 * Returns the sum of 2 numbers
 *
 * @example sum(1, 2)
 * //=> 3
 */
export const sum = (a, b) => {
  return a + b;
};

and my test:

//test/tests.js

import doctest from "@supabase/doctest-js";

describe("Doctests", () => {
  // file paths are relative to root of directory
  doctest("src/sum.js");
});

I installed mocha globally and ran npm test (after adding {..., "scripts": { "test": "mocha" } ... } in my package.json)

ran npm test and got this:

λ npm test

> misc@1.0.0 test C:\Users\logan\Projects\misc
> mocha

TypeError: doctest is not a function
    at Suite.<anonymous> (file:///C:/Users/logan/Projects/misc/test/tests.js:5:3)
    at Object.create (C:\Users\logan\AppData\Local\nvs\node\12.20.1\x64\node_modules\mocha\lib\interfaces\common.js:148:19)
    at context.describe.context.context (C:\Users\logan\AppData\Local\nvs\node\12.20.1\x64\node_modules\mocha\lib\interfaces\bdd.js:42:27)
    at file:///C:/Users/logan/Projects/misc/test/tests.js:3:1
    at ModuleJob.run (internal/modules/esm/module_job.js:145:37)
    at async Loader.import (internal/modules/esm/loader.js:182:24)
    at async Object.exports.loadFilesAsync (C:\Users\logan\AppData\Local\nvs\node\12.20.1\x64\node_modules\mocha\lib\esm-utils.js:33:20)
    at async singleRun (C:\Users\logan\AppData\Local\nvs\node\12.20.1\x64\node_modules\mocha\lib\cli\run-helpers.js:125:3)
    at async Object.exports.handler (C:\Users\logan\AppData\Local\nvs\node\12.20.1\x64\node_modules\mocha\lib\cli\run.js:362:5)
npm ERR! Test failed.  See above for more details.

I saw that you're doing some Windows path work. Could the error be due to that?

System information

erkeen commented 3 years ago

I have the same issue: TypeError: doctest is not a function on Windows 10 OS. I have tried to change version of Node.js, but still got the same error. It might be the issue with Windows OS.

linus commented 2 years ago

First off, thanks for a great module! I ❤️ doctests.

I don't think this is a Windows issue. My problem, at least, is that @supabase/doctest-js is not a proper ES module. The example states

import doctest from "@supabase/doctest-js";

When using that in a "type": "module" package, or a file with .mjs extension, Node tries to import it as an ES module, which fails (doctest will look like this:

{ default: [Function (anonymous)] }

And using e.g. doctest.default also doesn't work, since the module itself is distributed as a CommonJS module.

For more information, see https://nodejs.org/api/esm.html.

The workarounds today seems to be to either use require:

const { default: doctest } = require('@supabase/doctest-js');

Or (I haven't tested this) run your own code through e.g. babel, which will surely work its magic.

linus commented 2 years ago

Investigating further, it seems as if it would be possible to use import (while handling the .default member somehow on the consuming side) by changing the code here:

https://github.com/kiwicopple/doctest-js/blob/0e12599246ec71ab97a7ab804c017815a98e8dab/src/safe_eval.js#L7-L8

to use dynamic imports instead of require in the eval.