Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-issue]: Convert web-config-server tests to jest #4194

Merged
merged 10 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config-js.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"clearMocks": true,
"collectCoverageFrom": ["<rootDir>/src/**/*.js)"],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that nobody was checking test coverage in JS packages for the last 14 months 😛

"collectCoverageFrom": ["<rootDir>/src/**/*.js"],
"coveragePathIgnorePatterns": ["__tests__"],
"testMatch": ["<rootDir>/src/__tests__/**/**.test.js"],
"setupFilesAfterEnv": ["../../jest.setup.js"],
Expand Down
1 change: 1 addition & 0 deletions packages/central-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"mocha": "^8.1.3",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"sinon-chai": "^3.3.0",
"sinon-test": "^3.0.0",
"supertest": "^3.1.0"
}
Expand Down
10 changes: 1 addition & 9 deletions packages/database/.babelrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
const getPlugins = api => {
if (api.env(['development', 'test'])) {
return ['istanbul'];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build-related change, should be fine

}
return [];
};

const includedDateOffset = 90 * 24 * 60 * 60 * 1000; // include migrations up to 90 days old
const includedMigrationsDate = new Date().setTime(Date.now() - includedDateOffset);
const checkMigrationOutdated = function (migrationName) {
Expand Down Expand Up @@ -46,7 +39,6 @@ const getIgnore = api => {
};

module.exports = function (api) {
const plugins = getPlugins(api);
const ignore = getIgnore(api);
return { plugins, ignore };
return { ignore };
};
3 changes: 3 additions & 0 deletions packages/database/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const baseConfig = require('../../jest.config-js.json');

const { coveragePathIgnorePatterns = [] } = baseConfig;

module.exports = async () => ({
...baseConfig,
rootDir: '.',
coveragePathIgnorePatterns: [...coveragePathIgnorePatterns, '<rootDir>/src/migrations'],
setupFilesAfterEnv: ['../../jest.setup.js', './jest.setup.js'],
});
2 changes: 0 additions & 2 deletions packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@
},
"devDependencies": {
"@babel/node": "^7.10.5",
"cross-env": "^7.0.2",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"pluralize": "^8.0.0"
}
}
8 changes: 0 additions & 8 deletions packages/lesmis-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ aws-access-keys.js
# Icon must end with two \r
Icon


# Thumbnails
._*

Expand All @@ -33,7 +32,6 @@ Network Trash Folder
Temporary Items
.apdisk


### https://raw.github.com/github/gitignore/49d13cdba39774f7fa224ef13f4a1153200e2710/Node.gitignore

# Logs
Expand All @@ -53,9 +51,6 @@ lib-cov
# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

Expand Down Expand Up @@ -83,6 +78,3 @@ jspm_packages

# Yarn Integrity file
.yarn-integrity



8 changes: 0 additions & 8 deletions packages/psss-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ aws-access-keys.js
# Icon must end with two \r
Icon


# Thumbnails
._*

Expand All @@ -33,7 +32,6 @@ Network Trash Folder
Temporary Items
.apdisk


### https://raw.github.com/github/gitignore/49d13cdba39774f7fa224ef13f4a1153200e2710/Node.gitignore

# Logs
Expand All @@ -53,9 +51,6 @@ lib-cov
# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

Expand Down Expand Up @@ -83,6 +78,3 @@ jspm_packages

# Yarn Integrity file
.yarn-integrity



8 changes: 0 additions & 8 deletions packages/report-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ aws-access-keys.js
# Icon must end with two \r
Icon


# Thumbnails
._*

Expand All @@ -33,7 +32,6 @@ Network Trash Folder
Temporary Items
.apdisk


### https://raw.github.com/github/gitignore/49d13cdba39774f7fa224ef13f4a1153200e2710/Node.gitignore

# Logs
Expand All @@ -53,9 +51,6 @@ lib-cov
# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

Expand Down Expand Up @@ -83,6 +78,3 @@ jspm_packages

# Yarn Integrity file
.yarn-integrity



7 changes: 1 addition & 6 deletions packages/web-config-server/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@
}
}
]
],
"env": {
"test": {
"plugins": ["istanbul"]
}
}
]
}
2 changes: 1 addition & 1 deletion packages/web-config-server/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": [
"./src/tests/dataBuilderPercentagesByNominatedPair.test.js",
"./src/__tests__/dataBuilderPercentagesByNominatedPair.test.js",
"--require babel-register",
"--require @babel/polyfill",
"--timeout 100000",
Expand Down
20 changes: 20 additions & 0 deletions packages/web-config-server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const baseConfig = require('../../jest.config-js.json');

module.exports = async () => ({
...baseConfig,
rootDir: '.',
moduleNameMapper: {
'^/aggregator(.*)$': '<rootDir>/src/aggregator$1',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way to generalise these (without more elaborate folder scanning logic).

I also tried the following transformation. It throws an error because it is also applied to node_modules packages:

'^/(.*)$': '<rootDir>/src/$1'

'^/*apiV1(.*)$': '<rootDir>/src/apiV1$1',
'^/app(.*)$': '<rootDir>/src/app$1',
'^/appServer(.*)$': '<rootDir>/src/appServer$1',
'^/authSession(.*)$': '<rootDir>/src/authSession$1',
'^/connections(.*)$': '<rootDir>/src/connections$1',
'^/dhis(.*)$': '<rootDir>/src/dhis$1',
'^/export(.*)$': '<rootDir>/src/export$1',
'^/models(.*)$': '<rootDir>/src/models$1',
'^/preaggregation(.*)$': '<rootDir>/src/preaggregation$1',
'^/utils(.*)$': '<rootDir>/src/utils$1',
},
setupFilesAfterEnv: ['../../jest.setup.js', './jest.setup.js'],
});
25 changes: 25 additions & 0 deletions packages/web-config-server/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Tupaia
* Copyright (c) 2017 - 2022 Beyond Essential Systems Pty Ltd
*/

import { clearTestData, getTestDatabase } from '@tupaia/database';

import { getIsProductionEnvironment } from '/utils';

// These setup tasks need to be performed before any test, so we
// do them in this file outside of any describe block.

beforeAll(() => {
const isProductionEnvironment = getIsProductionEnvironment();
if (isProductionEnvironment) {
// Don't test on production
throw new Error('Never run the test suite on the production server, it messes with data!');
kael89 marked this conversation as resolved.
Show resolved Hide resolved
}
});

afterAll(async () => {
const database = getTestDatabase();
await clearTestData(database);
await database.closeConnections();
});
12 changes: 3 additions & 9 deletions packages/web-config-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"start": "node dist",
"start-dev": "yarn package:start:backend-start-dev 9997",
"start-verbose": "LOG_LEVEL=debug yarn start-dev",
"test": "yarn workspace @tupaia/database check-test-database-exists && DB_NAME=tupaia_test mocha",
"test:coverage": "cross-env NODE_ENV=test nyc mocha"
"test": "yarn package:test:withdb --runInBand",
"test:coverage": "yarn test --coverage"
},
"dependencies": {
"@tupaia/access-policy": "3.0.0",
Expand Down Expand Up @@ -69,12 +69,6 @@
},
"devDependencies": {
"babel-plugin-module-resolver": "^4.0.0",
"chai": "^4.1.2",
"chai-like": "^1.1.1",
"cross-env": "^7.0.2",
"csvtojson": "^1.1.5",
"mocha": "^8.1.3",
"nyc": "^15.1.0",
"sinon-chai": "^3.3.0"
"csvtojson": "^1.1.5"
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
/**
* Tupaia
* Copyright (c) 2017 - 2022 Beyond Essential Systems Pty Ltd
*/

import supertest from 'supertest';
import { util } from 'client-sessions';
import { createApp } from '/app';
import { USER_SESSION_CONFIG } from '/authSession';

export const DEFAULT_API_VERSION = 1;
const getVersionedEndpoint = (endpoint, apiVersion = DEFAULT_API_VERSION) =>
`/api/v${apiVersion}/${endpoint}`;

const app = createApp();

export class TestableApp {
constructor() {
constructor(app) {
this.app = app;
this.currentCookies = null;
}

async mockSessionUserJson(userName, email, accessPolicy) {
mockSessionUserJson(userName, email, accessPolicy) {
this.session = util.encode(USER_SESSION_CONFIG, {
userJson: {
userName,
Expand Down
Loading