Skip to content

Instantly share code, notes, and snippets.

@Nyoho
Created December 25, 2021 11:12
Show Gist options
  • Save Nyoho/91922b0b8c5912dba39734ea42b6166a to your computer and use it in GitHub Desktop.
Save Nyoho/91922b0b8c5912dba39734ea42b6166a to your computer and use it in GitHub Desktop.
Test npm scripts in package.json (WIP)
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const fetch = require('undici').fetch;
const promiseRetry = require('promise-retry');
const sleep = (time) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, time)
})
}
describe('npm scripts', () => {
it.concurrent('starts a dev server', async () => {
const ac = new AbortController();
const devServerPromise = exec('npm run dev', { signal: ac.signal });
const result = await promiseRetry(async (retry, number) => {
if (number > 1) {
console.log(`Retrying fetch...(${number})`);
}
try {
console.log(`Let's try (${number}th time)`);
return await fetch('http://localhost:3000/')
} catch (e) {
await sleep(1000);
await retry(e);
}
}, { retries: 10 });
console.log('After promiseRetry()');
try {
ac.abort();
} catch (e) {
console.log("abort 例外を無視 ", e);
}
expect(result.status).toBe(200);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment