Skip to content

Commit bdd3b5d

Browse files
authored
chore: Upgrades jest and related (#643)
* chore: upgrades `jest` and related * fix: updates config * chore: adapts custom environment
1 parent f3d8def commit bdd3b5d

5 files changed

+1328
-3357
lines changed

CustomEnvironment.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (c) Hathor Labs and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as path from 'path';
9+
10+
// eslint-disable-next-line import/no-extraneous-dependencies
11+
const NodeEnvironment = require('jest-environment-node').TestEnvironment;
12+
13+
/**
14+
* Extracts the test name from an absolute path received by the context
15+
* @param {string} filePath Absolute path
16+
* @returns {string} Test filename without directories, test suffixes or extensions
17+
* @example
18+
* const name = getTestName('/home/user/code/address-info.test.js')
19+
* assert(name == 'address-info')
20+
*/
21+
function getTestName(filePath) {
22+
const baseName = path.basename(filePath);
23+
const extName = path.extname(filePath);
24+
25+
return baseName.replace(`.test${extName}`, '');
26+
}
27+
28+
/**
29+
* This custom environment based on the Node environment is used to obtain the test name that is
30+
* currently being executed, an important piece of information used on `setupTests-integration.js`.
31+
* @see https://jestjs.io/docs/configuration#testenvironment-string
32+
*/
33+
export default class CustomEnvironment extends NodeEnvironment {
34+
/**
35+
* The testname is obtained from the constructor context
36+
* @param config
37+
* @param context
38+
*/
39+
constructor(config, context) {
40+
super(config, context);
41+
this.testName = getTestName(context.testPath);
42+
}
43+
44+
/**
45+
* The local testname is injected on the global environment for this specific test on setup
46+
* @returns {Promise<void>}
47+
*/
48+
async setup() {
49+
await super.setup();
50+
this.global.testName = this.testName;
51+
}
52+
53+
/*
54+
* For debugging purposes, some helper methods can be added to this class, such as:
55+
* - getVmContext()
56+
* - teardown()
57+
* - runScript(script)
58+
* - handleTestEvent(event)
59+
*
60+
* @see https://jestjs.io/docs/configuration#testenvironment-string
61+
*/
62+
}

jest-integration.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const mainTestMatch = process.env.SPECIFIC_INTEGRATION_TEST_FILE
44
: '<rootDir>/__tests__/integration/**/*.test.js';
55

66
module.exports = {
7-
testRunner: "jest-jasmine2",
7+
testRunner: 'jest-circus/runner',
88
clearMocks: true,
99
coverageDirectory: 'coverage-integration',
10-
testEnvironment: 'node',
10+
testEnvironment: './CustomEnvironment.js',
1111
collectCoverage: true,
1212
collectCoverageFrom: ['<rootDir>/src/**/*.js', '<rootDir>/src/**/*.ts'],
1313
testMatch: [mainTestMatch],

0 commit comments

Comments
 (0)