Skip to content

Commit

Permalink
Migrate to jest (#854)
Browse files Browse the repository at this point in the history
* Migrate to jest
* Use --force-exit until dns close handle issue jestjs/jest#9982

Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
  • Loading branch information
Christopher Kolstad and ivarconr committed May 28, 2021
1 parent 1d7be17 commit 3a65847
Show file tree
Hide file tree
Showing 110 changed files with 5,003 additions and 4,776 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"env": {
"node": true
"node": true,
"jest": true
},
"extends": [
"airbnb-typescript/base",
Expand Down
4 changes: 0 additions & 4 deletions .nycrc

This file was deleted.

59 changes: 29 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,41 @@
"build:watch": "tsc -w",
"build": "yarn run copy-templates && tsc --pretty",
"prepare": "yarn run build",
"test": "yarn build && NODE_ENV=test PORT=4243 ava",
"test": "NODE_ENV=test PORT=4243 jest",
"test:docker": "./scripts/docker-postgres.sh",
"test:watch": "yarn test --watch",
"test:coverage": "nyc --reporter=lcov yarn test",
"test:coverage-report": "nyc report --reporter=text-lcov | coveralls",
"test:coverage": "NODE_ENV=test PORT=4243 jest --coverage --forceExit",
"clean": "rimraf dist/"
},
"nyc": {
"all": true,
"include": [
"dist/lib/**/*.js"
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"testPathIgnorePatterns": [
"/dist/",
"/node_modules/"
],
"exclude": [
"dist/bin",
"dist/migrations"
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"exclude-after-remap": false
},
"ava": {
"files": [
"./dist/**/*.test.js",
"!src/**/*",
"!**/helpers/**/*",
"!**/fixtures/**/*"
],
"snapshotDir": "snapshots"
"coveragePathIgnorePatterns": [
"/node_modules/",
"/dist/",
"/src/migrations",
"/src/test"
]
},
"dependencies": {
"async": "^3.1.0",
"async": "^3.1.1",
"bcrypt": "^5.0.1",
"compression": "^1.7.3",
"compression": "^1.7.4",
"connect-session-knex": "^2.0.0",
"cookie-parser": "^1.4.4",
"cookie-parser": "^1.4.5",
"cookie-session": "^2.0.0-rc.1",
"cors": "^2.8.5",
"db-migrate": "0.11.12",
Expand All @@ -80,7 +82,7 @@
"helmet": "^4.1.0",
"joi": "^17.3.0",
"js-yaml": "^3.14.0",
"knex": "0.95.2",
"knex": "0.95.5",
"log4js": "^6.0.0",
"memoizee": "^0.4.15",
"mime": "^2.4.2",
Expand All @@ -101,16 +103,15 @@
"uuid": "^8.3.2"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.11",
"@types/jest": "^26.0.23",
"@types/node": "^15.6.0",
"@types/node-fetch": "^2.5.10",
"@types/nodemailer": "^6.4.1",
"@types/owasp-password-strength-test": "^1.3.0",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"ava": "^3.15.0",
"copyfiles": "^2.4.1",
"coveralls": "^3.1.0",
"eslint": "^6.8.0",
Expand All @@ -122,24 +123,22 @@
"faker": "^5.5.3",
"fetch-mock": "^9.11.0",
"husky": "^4.2.3",
"jest": "^27.0.1",
"lint-staged": "^11.0.0",
"lolex": "^6.0.0",
"nyc": "^15.1.0",
"prettier": "^1.19.1",
"proxyquire": "^2.1.3",
"rimraf": "^3.0.2",
"sinon": "^9.2.4",
"source-map-support": "^0.5.19",
"superagent": "^6.1.0",
"supertest": "^6.1.3",
"ts-jest": "^27.0.0",
"ts-node": "^9.1.1",
"tsc-watch": "^4.2.9",
"typescript": "^4.2.4"
},
"resolutions": {
"set-value": "^2.0.1",
"db-migrate/rc/minimist": "^1.2.5",
"nyc/**/minimist": "^1.2.5",
"knex/liftoff/object.map/**/kind-of": "^6.0.3",
"knex/liftoff/findup-sync/micromatc/kind-of": "^6.0.3",
"knex/liftoff/findup-sync/micromatc/nanomatch/kind-of": "^6.0.3",
Expand Down
Binary file removed snapshots/dist/lib/addons/slack.test.js.snap
Binary file not shown.
Binary file removed snapshots/src/lib/addons/datadog.test.js.snap
Binary file not shown.
Binary file removed snapshots/src/lib/addons/slack.test.js.snap
Binary file not shown.
Binary file removed snapshots/src/lib/addons/teams.test.js.snap
Binary file not shown.
5 changes: 5 additions & 0 deletions src/lib/addons/__snapshots__/datadog.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should call datadog webhook 1`] = `"{\\"text\\":\\"%%% \\\\n some@user.com created feature toggle [some-toggle](http://some-url.com/features/strategies/some-toggle)\\\\n**Enabled**: no | **Type**: undefined | **Project**: undefined\\\\n**Activation strategies**: \`\`\`- name: default\\\\n\`\`\` \\\\n %%% \\",\\"title\\":\\"Unleash notification update\\"}"`;

exports[`Should call datadog webhook for archived toggle 1`] = `"{\\"text\\":\\"%%% \\\\n The feature toggle *[some-toggle](http://some-url.com/archive/strategies/some-toggle)* was *archived* by some@user.com. \\\\n %%% \\",\\"title\\":\\"Unleash notification update\\"}"`;
5 changes: 5 additions & 0 deletions src/lib/addons/__snapshots__/slack.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should call slack webhook 1`] = `"{\\"username\\":\\"Unleash\\",\\"icon_emoji\\":\\":unleash:\\",\\"text\\":\\"some@user.com created feature toggle <http://some-url.com/features/strategies/some-toggle|some-toggle>\\\\n*Enabled*: no | *Type*: undefined | *Project*: undefined\\\\n*Activation strategies*: \`\`\`- name: default\\\\n\`\`\`\\",\\"channel\\":\\"#undefined\\",\\"attachments\\":[{\\"actions\\":[{\\"name\\":\\"featureToggle\\",\\"text\\":\\"Open in Unleash\\",\\"type\\":\\"button\\",\\"value\\":\\"featureToggle\\",\\"style\\":\\"primary\\",\\"url\\":\\"http://some-url.com/features/strategies/some-toggle\\"}]}]}"`;

exports[`Should call slack webhook for archived toggle 1`] = `"{\\"username\\":\\"Unleash\\",\\"icon_emoji\\":\\":unleash:\\",\\"text\\":\\"The feature toggle *<http://some-url.com/archive/strategies/some-toggle|some-toggle>* was *archived* by some@user.com.\\",\\"channel\\":\\"#undefined\\",\\"attachments\\":[{\\"actions\\":[{\\"name\\":\\"featureToggle\\",\\"text\\":\\"Open in Unleash\\",\\"type\\":\\"button\\",\\"value\\":\\"featureToggle\\",\\"style\\":\\"primary\\",\\"url\\":\\"http://some-url.com/archive/strategies/some-toggle\\"}]}]}"`;
5 changes: 5 additions & 0 deletions src/lib/addons/__snapshots__/teams.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should call teams webhook 1`] = `"{\\"themeColor\\":\\"0076D7\\",\\"summary\\":\\"Message\\",\\"sections\\":[{\\"activityTitle\\":\\"Feature toggle some-toggle | *Type*: undefined | *Project*: undefined <br /> *Activation strategies*: \\\\n- name: default\\\\n\\",\\"activitySubtitle\\":\\"Unleash notification update\\",\\"facts\\":[{\\"name\\":\\"User\\",\\"value\\":\\"some@user.com\\"},{\\"name\\":\\"Action\\",\\"value\\":\\"Create\\"},{\\"name\\":\\"Enabled\\",\\"value\\":\\"*no*\\"}]}],\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Go to feature\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"http://some-url.com/features/strategies/some-toggle\\"}]}]}"`;

exports[`Should call teams webhook for archived toggle 1`] = `"{\\"themeColor\\":\\"0076D7\\",\\"summary\\":\\"Message\\",\\"sections\\":[{\\"activityTitle\\":\\"The feature toggle *some-toggle* was *archived*\\",\\"activitySubtitle\\":\\"Unleash notification update\\",\\"facts\\":[{\\"name\\":\\"User\\",\\"value\\":\\"some@user.com\\"},{\\"name\\":\\"Action\\",\\"value\\":\\"feature-archived\\"},{\\"name\\":\\"Enabled\\",\\"value\\":\\"*no*\\"}]}],\\"potentialAction\\":[{\\"@type\\":\\"OpenUri\\",\\"name\\":\\"Go to feature\\",\\"targets\\":[{\\"os\\":\\"default\\",\\"uri\\":\\"http://some-url.com/archive/strategies/some-toggle\\"}]}]}"`;
29 changes: 15 additions & 14 deletions src/lib/addons/addon.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const test = require('ava');
const proxyquire = require('proxyquire');
const lolex = require('lolex');
const fetchMock = require('fetch-mock').sandbox();
const noLogger = require('../../test/fixtures/no-logger');

const Addon = proxyquire('./addon', { 'node-fetch': fetchMock });
jest.mock('node-fetch', () => fetchMock);

test.beforeEach(() => {
const Addon = require('./addon');

beforeEach(() => {
fetchMock.restore();
fetchMock.reset();
});
Expand All @@ -17,9 +16,9 @@ const definition = {
description: 'hello',
};

test.serial('Retries if fetch throws', async t => {
test('Retries if fetch throws', async () => {
const url = 'https://test.some.com';
const clock = lolex.install();
jest.useFakeTimers('modern');
const addon = new Addon(definition, {
getLogger: noLogger,
});
Expand All @@ -43,13 +42,14 @@ test.serial('Retries if fetch throws', async t => {
201,
);
await addon.fetchRetry(url);
clock.tick(1000);
t.true(fetchMock.done());
jest.advanceTimersByTime(1000);
expect(fetchMock.done()).toBe(true);
jest.useRealTimers();
});

test.serial('does not crash even if fetch throws', async t => {
test('does not crash even if fetch throws', async () => {
const url = 'https://test.some.com';
const clock = lolex.install();
jest.useFakeTimers('modern');
const addon = new Addon(definition, {
getLogger: noLogger,
});
Expand All @@ -64,7 +64,8 @@ test.serial('does not crash even if fetch throws', async t => {
},
);
await addon.fetchRetry(url);
clock.tick(1000);
t.true(fetchMock.done());
t.is(fetchMock.calls().length, 2);
jest.advanceTimersByTime(1000);
expect(fetchMock.done()).toBe(true);
expect(fetchMock.calls()).toHaveLength(2);
jest.useRealTimers();
});
46 changes: 24 additions & 22 deletions src/lib/addons/datadog.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
const test = require('ava');
const proxyquire = require('proxyquire').noCallThru();
const { FEATURE_CREATED, FEATURE_ARCHIVED } = require('../types/events');

const DatadogAddon = proxyquire.load('./datadog', {
'./addon': class Addon {
constructor(definition, { getLogger }) {
this.logger = getLogger('addon/test');
this.fetchRetryCalls = [];
}
jest.mock(
'./addon',
() =>
class Addon {
constructor(definition, { getLogger }) {
this.logger = getLogger('addon/test');
this.fetchRetryCalls = [];
}

async fetchRetry(url, options, retries, backoff) {
this.fetchRetryCalls.push({ url, options, retries, backoff });
return Promise.resolve({ status: 200 });
}
},
});
async fetchRetry(url, options, retries, backoff) {
this.fetchRetryCalls.push({ url, options, retries, backoff });
return Promise.resolve({ status: 200 });
}
},
);

const DatadogAddon = require('./datadog');

const noLogger = require('../../test/fixtures/no-logger');

test('Should call datadog webhook', async t => {
test('Should call datadog webhook', async () => {
const addon = new DatadogAddon({
getLogger: noLogger,
unleashUrl: 'http://some-url.com',
Expand All @@ -38,12 +40,12 @@ test('Should call datadog webhook', async t => {
};

await addon.handleEvent(event, parameters);
t.is(addon.fetchRetryCalls.length, 1);
t.is(addon.fetchRetryCalls[0].url, parameters.url);
t.snapshot(addon.fetchRetryCalls[0].options.body);
expect(addon.fetchRetryCalls.length).toBe(1);
expect(addon.fetchRetryCalls[0].url).toBe(parameters.url);
expect(addon.fetchRetryCalls[0].options.body).toMatchSnapshot();
});

test('Should call datadog webhook for archived toggle', async t => {
test('Should call datadog webhook for archived toggle', async () => {
const addon = new DatadogAddon({
getLogger: noLogger,
unleashUrl: 'http://some-url.com',
Expand All @@ -61,7 +63,7 @@ test('Should call datadog webhook for archived toggle', async t => {
};

await addon.handleEvent(event, parameters);
t.is(addon.fetchRetryCalls.length, 1);
t.is(addon.fetchRetryCalls[0].url, parameters.url);
t.snapshot(addon.fetchRetryCalls[0].options.body);
expect(addon.fetchRetryCalls.length).toBe(1);
expect(addon.fetchRetryCalls[0].url).toBe(parameters.url);
expect(addon.fetchRetryCalls[0].options.body).toMatchSnapshot();
});

0 comments on commit 3a65847

Please sign in to comment.