kiwicopple / doctest-js Goto Github PK
View Code? Open in Web Editor NEWRun JSDoc style doc examples as tests within your test suite
License: MIT License
Run JSDoc style doc examples as tests within your test suite
License: MIT License
This library is basically just assertions, so Baretest might be better for performance:
Running npm run build
has two issues:
[webpack-cli] Unknown argument: -p
And:
[webpack-cli] Compilation finished
assets by status 1.52 KiB [cached] 1 asset
./src/index.js 39 bytes [built] [code generated] [1 error]
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Plugin/Preset files are not allowed to export objects, only functions. In /Users/linus.thiel/development/linus/doctest-js/node_modules/babel-preset-es2015/lib/index.js
at createDescriptor (/Users/linus.thiel/development/linus/doctest-js/node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
at /Users/linus.thiel/development/linus/doctest-js/node_modules/@babel/core/lib/config/config-descriptors.js:109:50
at Array.map (<anonymous>)
at createDescriptors (/Users/linus.thiel/development/linus/doctest-js/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
at createPresetDescriptors (/Users/linus.thiel/development/linus/doctest-js/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
at presets (/Users/linus.thiel/development/linus/doctest-js/node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
at mergeChainOpts (/Users/linus.thiel/development/linus/doctest-js/node_modules/@babel/core/lib/config/config-chain.js:384:26)
at /Users/linus.thiel/development/linus/doctest-js/node_modules/@babel/core/lib/config/config-chain.js:347:7
at Generator.next (<anonymous>)
at buildRootChain (/Users/linus.thiel/development/linus/doctest-js/node_modules/@babel/core/lib/config/config-chain.js:129:29)
Expected result:
Build succeeds and a new file is produced in dist/index.js
.
Thanks for the lovely package.
Pondering...
Understandably, Jest appears to only ever point its errors at the evalValue()
function inside safe_eval.js.
Does Jest have a mechanism for specifying the file & line of code where the @example
test came from?
It would be a bit nicer if the fail messages from Jest et al. pointed users to the source @example of the trouble.
Jest's expect.extend() might be part of the puzzle.
Currently all failed tests appear to lead here...
FAIL src/helpers/index.test.js
Test suite failed to run
SyntaxError: Unexpected identifier
11 |
12 | describe('Doctests', () => {
> 13 | doctest('src/helpers/array.js', { testingFunction: testFn })
| ^
14 | })
at evalValue (node_modules/@supabase/doctest-js/dist/index.js:571:100047)
at exports.default (node_modules/@supabase/doctest-js/dist/index.js:571:100487)
at node_modules/@supabase/doctest-js/dist/index.js:124:31475
at Array.forEach (<anonymous>)
at t.default (node_modules/@supabase/doctest-js/dist/index.js:124:31433)
at src/helpers/index.test.js:13:3
at Object.<anonymous> (src/helpers/index.test.js:12:1)
I may be doing something stupid, but I'm getting this even on the README example using jest:
test("should ", () => {
expect("x").toBe("x")
doctest("src/sum.js") //?
})
results from Jest runner:
TypeError: doctest is not a function
3 | test("should ", () => {
4 | expect("x").toBe("x")
> 5 | doctest("src/sum.js") //?
| ^
6 | })
7 |
It would be great if this repo ran the tests every time it received a new push.
See here for an example
@example LoadWidget() //=> Promise.resolve(true)
returns
โ Doctests โบ doctest: LoadWidget()
Expected value {}
Received:
{}
Message:
expected {} to deeply equal {}
Am I doing something wrong or is this not supported??
A tested function which incorrectly returns any falsy value (0
, false
, ""
, null
, undefined
, etc) is considered passing by @supabase/doctest-js
.
When a function under test returns a falsy value, even if the doctest @example specifies a particular, different, value, the test passes.
Example function:
/**
* @example sum(1, 2)
* //=> 3
*/
export const sum = (a, b) => {
return null;
}
And the test
import doctest from "@supabase/doctest-js";
describe("Doctests", () => {
// file paths are relative to root of directory
doctest("src/sum.js");
});
This passes (at least, when run through mocha
).
The expected result would be that when a function under test returns anything other than the result specified in the @example, the test fails.
OS: MacOS Big Sur, 11.6.1
Node version: v14.18.0
@supabase/doctest-js version: 0.1.0
mocha version: 9.1.3
this seems to be failing:
/**
* @param {object} obj
* @private
* @returns {string}
*
* @example
* objectToQueryString({
* param1: 'hello',
* param2: 'world
* })
* //=>
* 'param1=hello¶m2=world'
*/
export function objectToQueryString(obj) {
return Object.keys(obj)
.map(param => `${param}=${this.obj[param]}`)
.join('&')
}
Great that you're picking this up! Just curious about your experience with the code base so far...
TypeError: doctest is not a function
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.