doctest-js
doctest-js copied to clipboard
`doctest is not a function`
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
> [email protected] 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
- OS: Window s10
- Version of supabase-js: "@supabase/doctest-js": "^0.1.0"
- Version of Node.js: Node 12.x
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.
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.
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
.