coderaiser / supertape

📼 Simplest high speed testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is this useable with TypeScript?

tommy-mitchell opened this issue · comments

commented

I'm not very familiar with testing in the JS ecosystem, and while setting up a new project I found that I could use Tape to test with:

ts-node node_modules/tape/bin/tape tests/**/*.ts

I like the features of Supertape, but I can't seem to get it to run my tests. I've tried both ts-node and esno/esmo, but I only get a "Done in 0.XXs" output without showing the testing results. Is there something I'm doing wrong, or is this use case not viable?

Looks like TS cannot work when:

{
    "type": "module"
}

Set in package.json. If you remove it, it will work good:

coderaiser@localcmd:~/escover/test$ ts-node test.ts 
TAP version 13
# hello: world
ok 1 should equal

1..1
# tests 1
# pass 1

# ok

coderaiser@localcmd:~/escover/test$ cat test.ts 
import {
    test,
    Test,
} from 'supertape';

test('hello: world', (t: Test) => {
    t.equal(1, 1);
    t.end();
});

So yes, 📼Supertape compatible with TS and have types, anyways TypeScript not really compatible with node platform, and have problems with dynamic imports, for example.

commented

I can run tests with ts-node myTestScript.ts, but I don't know how to configure Supertape this way. For example, to set the CHECK_ASSERTIONS_COUNT environment variable. Assuming that myTestScript.ts has import test from "supertape", I've tried:

  • ts-node myTestScript.ts -- --no-check-assertions-count
  • set CHECK_ASSERTIONS_COUNT=0 && ts-node myTestScript.ts
  • set SUPERTAPE_CHECK_ASSERTIONS_COUNT=0 && ts-node myTestScript.ts
  • set NODE_ENV=CHECK_ASSERTIONS_COUNT=0 && ts-node myTestScript.ts
  • set NODE_ENV='CHECK_ASSERTIONS_COUNT=0' && ts-node myTestScript.ts
  • set NODE_ENV=SUPERTAPE_CHECK_ASSERTIONS_COUNT=0 && ts-node myTestScript.ts
  • set NODE_ENV='SUPERTAPE_CHECK_ASSERTIONS_COUNT=0' && ts-node myTestScript.ts

None of these have worked. Is there an options file I can use, or a way to set these variables in myTestScript.ts?

Fixed with ef05768. Landed in v7.1.1 🎉 , is it works for you?

commented

Seems I'm still having an issue with TypeScript/ts-node, but not with supertape test.js. The environment variable is being passed correctly in both, though.

TypeScript:

import test from "supertape";

const add = (one: number, two: number): number => one + two;

test("add: multiple", t => {
    t.comment(`SUPERTAPE_CHECK_ASSERTIONS_COUNT: ${process.env.SUPERTAPE_CHECK_ASSERTIONS_COUNT as string}`);

    t.equal(add(1, 1), 2);
    t.equal(add(1, 2), 3);
    t.equal(add(2, 2), 4);

    t.end();
});

Output:

TAP version 13
# add: multiple
# SUPERTAPE_CHECK_ASSERTIONS_COUNT: 0
ok 1 should equal
ok 2 should equal
ok 3 should equal
not ok 4 Only one assertion per test allowed, looks like you have more
  ---
    operator: fail
    at file:///E:/src/js/booper/src/tests/super.ts:5:1
    stack: |-
      Error: Only one assertion per test allowed, looks like you have more
          at run (file:///E:/src/js/booper/node_modules/supertape/lib/operators.mjs:267:33)
          at Object.fail (file:///E:/src/js/booper/node_modules/supertape/lib/operators.mjs:196:16)
          at runOneTest (E:\src\js\booper\node_modules\supertape\lib\run-tests.js:169:15)
          at async runTests (E:\src\js\booper\node_modules\supertape\lib\run-tests.js:87:9)
          at async module.exports (E:\src\js\booper\node_modules\supertape\lib\run-tests.js:51:12)
          at async EventEmitter.<anonymous> (E:\src\js\booper\node_modules\supertape\lib\supertape.js:71:26)
  ...

1..4
# tests 4
# pass 3
# fail 1

JS:

import test from "supertape";

const add = (one, two) => one + two;

test("add: multiple", t => {
    t.comment(`SUPERTAPE_CHECK_ASSERTIONS_COUNT: ${process.env.SUPERTAPE_CHECK_ASSERTIONS_COUNT}`);

    t.equal(add(1, 1), 2);
    t.equal(add(1, 2), 3);
    t.equal(add(2, 2), 4);

    t.end();
});

Output:

# SUPERTAPE_CHECK_ASSERTIONS_COUNT: 0
TAP version 13

1..3
# tests 3
# pass 3

# ✅ ok

Thanks for the quick responses.

As I see you are using windows, could you please try to pass a flag:

tape ––no-check-assertions-count super.ts 

Currently Env variable supported only when you running test using supertape, if you want to disable check you can pass options object:

import {test} from "supertape";

const add = (one: number, two: number): number => one + two;

test("add: multiple", t => {
    t.comment(`SUPERTAPE_CHECK_ASSERTIONS_COUNT: ${process.env.SUPERTAPE_CHECK_ASSERTIONS_COUNT as string}`);

    t.equal(add(1, 1), 2);
    t.equal(add(1, 2), 3);
    t.equal(add(2, 2), 4);

    t.end();
}, {
    checkAssertionsCount: false
});
coderaiser@localcmd:~/supertape$ ts-node 1.ts
TAP version 13
# add: multiple
# SUPERTAPE_CHECK_ASSERTIONS_COUNT: undefined
ok 1 should equal
ok 2 should equal
ok 3 should equal

1..3
# tests 3
# pass 3

# ok

You can add wrapper:

import {test as _test} from 'supertape';
const test = (message, fn) => _test(message, fn, {
    checkAssertionsCount: false,
});
commented

The wrapper worked perfectly, thank you!

commented

Adding another comment to say: it could be helpful to have the options feature documented on the ReadMe, as of right now I can't find anything describing its usage.

Yes, it would be great! Are you willing for a PR :)?

commented

I wouldn't mind helping out, can you point me to where I can find the configuration feature(s)?

I think before example would be a good place.

commented

Sorry, I meant where in the code I can find it.

Sorry, I meant where in the code I can find it.

const defaultOptions = {
skip: false,
only: false,
extensions: {},
quiet: false,
format: 'tap',
run: true,
getOperators,
isStop: () => false,
checkDuplicates: true,
checkIfEnded: true,
checkAssertionsCount: true,
checkScopes: true,
};