coderaiser / supertape

📼 Simplest high speed testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

My test needs startup time before supertape detects tests

cloudrac3r opened this issue · comments

(Thank you very much for making supertape, it's the only testing library that matches how my brain works!)

I'm having a problem where supertape is stopping execution before my tests start running. In my test file, I want to do some slow setup (download some files to use in the tests) before the tests execute. But supertape stops it early before it can finish the setup.

From digging into the code, it seems this is because supertape expects one test() to be called in the test file straight away, and then loops every 5 ms until new test()s stop being added. Then it runs all of those tests.

So my test file basically looks like this:

const {test} = require("supertape")
const fetch = require("node-fetch")

const file = await fetch("some url").then(res => res.buffer()) // supertape stops trying to detect tests and stops the tests while this is downloading

test("process: I can process the file", () => {
  t.equal(processFile(file), "expected result")
})

I'd love it if supertape would let me do some setup before I do the tests. I can think of three ways to do this:

  • (a) supertape could wait for my test file to get to at least one test() rather than ending it early
  • (b) OR, supertape could have a setup(async () => { ... }) function that I can put my setup code in, and supertape will wait for setup before detecting tests
  • (c) OR, supertape could have a go() function that I call once I've finished my setup

Do you have any advice on what I can do to make my use case work, or would I have to figure out a different way of doing it?

Try to use to increase load loop time with env variable SUPERTAPE_LOAD_LOOP_TIMEOUT, it is 5 by default but can be bigger:

SUPERTAPE_LOAD_LOOP_TIMEOUT=100 tape 'test/**/*.js'

It is made to have ability to run tests in both ways: with test run runner, and with node test.js.

Also you can use:

let file;

test('setup', async (t) => {
   file = await fetch("some url").then(res => res.buffer())
}, {checkAssertionsCount: false});

You can even hide this logic to extend and put in separate file setup.js:

import {extend} from 'supertape';

let file;
export const test = extend({
    matchFetch: async (operator) = (result) => {
       file = await fetch("some url").then(res => res.buffer())
    };
    return operator.equal(file, result);
});

test('example', async(t) => {
    await t.matchFetch('hello');
});

Is it works for you?

Thanks for the quick response! I've been testing this but ran into some confusion:

var file
test('setup', async (t) => {
   file = await fetch("some url").then(res => res.buffer())
}, {checkAssertionsCount: false});
test('the real test', t => {
 // do something with file
})

This seems to work well on supertape version 9 because "the real test" waits until the "setup" test has finished and downloaded the file.

On supertape version 10, the tests will not wait for each other unless I add --no-worker. Is this intended?