Migrate from karma+mocha to jest

This commit is contained in:
SleepWalker 2019-11-27 10:09:51 +02:00
parent 06597c482a
commit 991031f211
15 changed files with 1272 additions and 1109 deletions

View File

@ -0,0 +1,22 @@
/* eslint-env node */
const path = require("path");
const { transform } = require("../../webpack-utils/intl-loader");
module.exports = {
/**
* @param {string} src - transformed module source code
* @param {string} filename - transformed module file path
* @param {{[key: string]: any}} config - jest config
* @param {{instrument: boolean}} options - additional options
*
* @return {string}
*/
// eslint-disable-next-line no-unused-vars
process(src, filename, config, options) {
return transform(
src,
filename,
path.resolve(`${__dirname}/../../..`)
);
}
};

View File

@ -0,0 +1 @@
module.exports = 'test-file-stub';

View File

@ -21,8 +21,3 @@ if (!window.localStorage) {
...window.localStorage ...window.localStorage
}; };
} }
// require all modules ending in ".test.js" from the
// current directory and all subdirectories
const testsContext = require.context('.', true, /\.test\.jsx?$/);
testsContext.keys().forEach(testsContext);

View File

@ -1,83 +0,0 @@
/* eslint-env node */
// https://docs.gitlab.com/ce/ci/variables/README.html
// noinspection Eslint
const isCi = typeof process.env.CI !== 'undefined';
module.exports = function(config) {
const params = {
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'sinon'],
// list of files / patterns to load in the browser
files: [
'dll/vendor.dll.js',
'src/test.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'src/test.js': ['webpack', 'sourcemap']
},
webpack: require('./webpack.config.js'),
webpackServer: {
noInfo: true // please don't spam the console when running in karma!
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['nyan'],
nyanReporter: {
// suppress the red background on errors in the error
// report at the end of the test run
suppressErrorHighlighting: true
},
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['jsdom'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
};
if (isCi) {
Object.assign(params, {
reporters: ['dots'],
autoWatch: false,
singleRun: true,
client: {
captureConsole: false
}
});
}
config.set(params);
};

View File

@ -28,7 +28,7 @@
"start": "yarn run clean && yarn run build:dll && NODE_PATH=./src webpack-dev-server --colors", "start": "yarn run clean && yarn run build:dll && NODE_PATH=./src webpack-dev-server --colors",
"clean": "rm -rf ./dist && mkdir ./dist", "clean": "rm -rf ./dist && mkdir ./dist",
"e2e": "yarn --cwd ./tests-e2e test", "e2e": "yarn --cwd ./tests-e2e test",
"test": "yarn run build:dll && NODE_PATH=./src karma start ./karma.conf.js", "test": "NODE_PATH=./src jest",
"analyze": "yarn run clean && yarn run build:webpack --analyze", "analyze": "yarn run clean && yarn run build:webpack --analyze",
"lint": "eslint ./src", "lint": "eslint ./src",
"flow": "flow", "flow": "flow",
@ -42,6 +42,29 @@
"build:dll": "node ./scripts/build-dll.js", "build:dll": "node ./scripts/build-dll.js",
"build:serve": "http-server --proxy https://dev.account.ely.by ./dist" "build:serve": "http-server --proxy https://dev.account.ely.by ./dist"
}, },
"jest": {
"roots": [
"<rootDir>/src/"
],
"setupFilesAfterEnv": [
"<rootDir>/jest/setupAfterEnv.js"
],
"resetMocks": true,
"resetModules": true,
"restoreMocks": true,
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/jest/__mocks__/mockStrExport.js",
"\\.(css|less|scss)$": "identity-obj-proxy"
},
"transform": {
"\\.intl\\.json$": "<rootDir>/jest/__mocks__/intlMock.js",
"^.+\\.[tj]sx?$": "babel-jest"
}
},
"dependencies": { "dependencies": {
"@formatjs/intl-pluralrules": "^1.3.7", "@formatjs/intl-pluralrules": "^1.3.7",
"@formatjs/intl-relativetimeformat": "^4.4.6", "@formatjs/intl-relativetimeformat": "^4.4.6",
@ -116,19 +139,13 @@
"flow-bin": "~0.102.0", "flow-bin": "~0.102.0",
"html-loader": "^0.5.5", "html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0", "html-webpack-plugin": "^3.2.0",
"identity-obj-proxy": "^3.0.0",
"imports-loader": "^0.8.0", "imports-loader": "^0.8.0",
"jsdom": "^15.2.1", "jest": "^24.9.0",
"jest-watch-typeahead": "^0.4.2",
"json-loader": "^0.5.4", "json-loader": "^0.5.4",
"karma": "^4.4.1",
"karma-jsdom-launcher": "^8.0.0",
"karma-mocha": "^1.0.0",
"karma-nyan-reporter": "^0.2.3",
"karma-sinon": "^1.0.4",
"karma-sourcemap-loader": "*",
"karma-webpack": "^4.0.2",
"loader-utils": "^1.0.0", "loader-utils": "^1.0.0",
"mini-css-extract-plugin": "^0.8.0", "mini-css-extract-plugin": "^0.8.0",
"mocha": "^6.2.2",
"node-sass": "^4.13.0", "node-sass": "^4.13.0",
"postcss-import": "^12.0.1", "postcss-import": "^12.0.1",
"postcss-loader": "^3.0.0", "postcss-loader": "^3.0.0",

View File

@ -1,2 +0,0 @@
describe('components/user/actions', () => {
});

View File

@ -51,7 +51,7 @@ describe('AcceptRulesState', () => {
describe('#resolve', () => { describe('#resolve', () => {
it('should call acceptRules', () => { it('should call acceptRules', () => {
expectRun(mock, 'acceptRules') expectRun(mock, 'acceptRules')
.returns(Promise.resolve()); .returns(new Promise(() => {}));
state.resolve(context); state.resolve(context);
}); });

View File

@ -63,7 +63,7 @@ describe('ActivationState', () => {
mock, mock,
'activate', 'activate',
sinon.match.same(payload) sinon.match.same(payload)
).returns(Promise.resolve()); ).returns(new Promise(() => {}));
state.resolve(context, payload); state.resolve(context, payload);
}); });

View File

@ -41,7 +41,7 @@ describe('ForgotPasswordState', () => {
sinon.match({ sinon.match({
login: expectedLogin login: expectedLogin
}) })
).returns(Promise.resolve()); ).returns(new Promise(() => {}));
state.resolve(context, {login: expectedLogin}); state.resolve(context, {login: expectedLogin});
}); });

View File

@ -59,7 +59,7 @@ describe('LoginState', () => {
mock, mock,
'login', 'login',
sinon.match.same(payload) sinon.match.same(payload)
).returns(Promise.resolve()); ).returns(new Promise(() => {}));
state.resolve(context, payload); state.resolve(context, payload);
}); });

View File

@ -53,7 +53,7 @@ describe('RecoverPasswordState', () => {
mock, mock,
'recoverPassword', 'recoverPassword',
sinon.match(expectedPayload) sinon.match(expectedPayload)
).returns(Promise.resolve()); ).returns(new Promise(() => {}));
state.resolve(context, expectedPayload); state.resolve(context, expectedPayload);
}); });

View File

@ -44,7 +44,7 @@ describe('RegisterState', () => {
mock, mock,
'register', 'register',
sinon.match.same(payload) sinon.match.same(payload)
).returns(Promise.resolve()); ).returns(new Promise(() => {}));
state.resolve(context, payload); state.resolve(context, payload);
}); });

View File

@ -58,7 +58,7 @@ describe('ResendActivationState', () => {
mock, mock,
'resendActivation', 'resendActivation',
sinon.match.same(payload) sinon.match.same(payload)
).returns(Promise.resolve()); ).returns(new Promise(() => {}));
state.resolve(context, payload); state.resolve(context, payload);
}); });

View File

@ -1,9 +1,8 @@
module.exports = function(content) { function transform(src, modulePath, rootContext) {
this.cacheable && this.cacheable(); const json = JSON.parse(src);
content = JSON.parse(content);
const moduleId = this.context const moduleId = modulePath
.replace(this.rootContext, '') .replace(rootContext, '')
// TODO: can't find the way to strip out this path part programmatically // TODO: can't find the way to strip out this path part programmatically
// this is a directory from resolve.modules config // this is a directory from resolve.modules config
// may be this may work: .replace(this._compiler.options.resolve.root, '') // may be this may work: .replace(this._compiler.options.resolve.root, '')
@ -11,20 +10,28 @@ module.exports = function(content) {
.replace(/^\/|\/$/g, '') .replace(/^\/|\/$/g, '')
.replace(/\//g, '.'); .replace(/\//g, '.');
content = JSON.stringify( return JSON.stringify(
Object.keys(content).reduce( Object.keys(json).reduce(
(translations, key) => ({ (translations, key) => ({
...translations, ...translations,
[key]: { [key]: {
id: `${moduleId}.${key}`, id: `${moduleId}.${key}`,
defaultMessage: content[key] defaultMessage: json[key]
} }
}), }),
{} {}
) )
); );
}
module.exports = function(content) {
this.cacheable && this.cacheable();
content = transform(content, this.context, this.rootContext);
return `import { defineMessages } from 'react-intl'; return `import { defineMessages } from 'react-intl';
export default defineMessages(${content})`; export default defineMessages(${content})`;
}; };
module.exports.transform = transform;

2194
yarn.lock

File diff suppressed because it is too large Load Diff