📝 Increase readability of snapshot tests

* Translate to typescript.
* Use explicit imports for mocha and jest.
* Remove dependency on @types/mocha as it may conflict with jest types.
* Upgrade jest & replace yaml-jest with custom transformer.
* Fix mocha-webpack bug.
* Prevent jest from priting console outputs.
pull/1689/head
Alexandre Hajjar 2021-07-21 23:55:08 +02:00
parent eaf463cc29
commit c9d6d3d7dd
9 changed files with 1182 additions and 837 deletions

View File

@ -132,7 +132,7 @@ module.exports = {
// testLocationInResults: false,
// The glob patterns Jest uses to detect test files
testMatch: ['**/*.jest.js'],
testMatch: ['**/*.jest.ts'],
// [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
@ -163,7 +163,7 @@ module.exports = {
// It's not possible to have 2 piped transformers like in webpack
// ie ['jest-transform-nearley', 'babel-jest'], so we removed ES6 module from nearley output.
'\\.ne$': require.resolve('jest-transform-nearley'),
'\\.yaml$': require.resolve('yaml-jest'),
'\\.yaml$': 'mon-entreprise/test/regressions/yaml-transformer.js',
'\\.(js|tsx?)$': require.resolve('babel-jest'),
},

View File

@ -1,4 +1,5 @@
import { expect } from 'chai'
import { describe, it, beforeEach, afterEach } from 'mocha'
import { createMemoryHistory } from 'history'
import { DottedName } from 'modele-social'
import { createStore } from 'redux'

View File

@ -6,8 +6,11 @@
// renamed the test configuration may be adapted but the persisted snapshot will remain unchanged).
/* eslint-disable no-undef */
import rules from '../../../modele-social'
import { expect, it } from '@jest/globals'
import rules, { DottedName } from '../../../modele-social'
import { engineFactory } from '../../source/components/utils/EngineContext'
import { Simulation } from '../../source/reducers/rootReducer'
import aideDéclarationConfig from '../../source/pages/Gérer/AideDéclarationIndépendant/config.yaml'
import artisteAuteurConfig from '../../source/pages/Simulateurs/configs/artiste-auteur.yaml'
import autoentrepreneurConfig from '../../source/pages/Simulateurs/configs/auto-entrepreneur.yaml'
@ -24,10 +27,15 @@ import professionsLibéralesSituations from './simulations-professions-libérale
import remunerationDirigeantSituations from './simulations-rémunération-dirigeant.yaml'
import employeeSituations from './simulations-salarié.yaml'
const roundResult = (arr) => arr.map((x) => Math.round(x))
type SituationsSpecs = Record<string, Simulation['situation'][]>
const roundResult = (arr: number[]) => arr.map((x) => Math.round(x))
const engine = engineFactory(rules)
const runSimulations = (situations, targets, baseSituation = {}) =>
Object.entries(situations).map(([name, situations]) =>
const runSimulations = (
situationsSpecs: SituationsSpecs,
objectifs: DottedName[],
baseSituation: Simulation['situation'] = {}
) =>
Object.entries(situationsSpecs).map(([name, situations]) =>
situations.forEach((situation) => {
Object.keys(situation).forEach((situationRuleName) => {
// TODO: This check may be moved in the `engine.setSituation` method
@ -38,7 +46,9 @@ const runSimulations = (situations, targets, baseSituation = {}) =>
}
})
engine.setSituation({ ...baseSituation, ...situation })
const res = targets.map((target) => engine.evaluate(target).nodeValue)
const res = objectifs.map(
(objectif) => engine.evaluate(objectif).nodeValue
)
const evaluatedNotifications = Object.values(engine.getParsedRules())
.filter(
@ -55,7 +65,8 @@ const runSimulations = (situations, targets, baseSituation = {}) =>
// line in the snapshot, which considerably reduce the number of lines of this snapshot
// and improve its readability.
expect(
JSON.stringify(roundResult(res)) + snapshotedDisplayedNotifications
JSON.stringify(roundResult(res as number[])) +
snapshotedDisplayedNotifications
).toMatchSnapshot(name)
})
)
@ -69,7 +80,7 @@ it('calculate simulations-salarié', () => {
})
it('calculate simulations-indépendant', () => {
const targets = [
const objectifs = [
'dirigeant . rémunération . totale',
'dirigeant . rémunération . cotisations',
'dirigeant . rémunération . nette',
@ -79,8 +90,8 @@ it('calculate simulations-indépendant', () => {
'entreprise . charges',
"entreprise . chiffre d'affaires",
'dirigeant . indépendant . cotisations et contributions . début activité',
]
runSimulations(independentSituations, targets, independantConfig.situation)
] as DottedName[]
runSimulations(independentSituations, objectifs, independantConfig.situation)
})
it('calculate simulations-auto-entrepreneur', () => {
@ -98,8 +109,7 @@ it('calculate simulations-rémunération-dirigeant (assimilé salarié)', () =>
{
...remunerationDirigeantConfig.situation,
dirigeant: "'assimilé salarié'",
},
'assimilé salarié'
}
)
})
@ -110,8 +120,7 @@ it('calculate simulations-rémunération-dirigeant (auto-entrepreneur)', () => {
{
...remunerationDirigeantConfig.situation,
dirigeant: "'auto-entrepreneur'",
},
'auto-entrepreneur'
}
)
})
@ -122,8 +131,7 @@ it('calculate simulations-rémunération-dirigeant (indépendant)', () => {
{
...remunerationDirigeantConfig.situation,
dirigeant: "'indépendant'",
},
'indépendant'
}
)
})

View File

@ -0,0 +1,20 @@
// Inspired from yaml-jest https://github.com/danwang/yaml-jest
var crypto = require('crypto')
var yaml = require('js-yaml')
const getCacheKey = (fileData, filePath, options) => {
return crypto
.createHash('md5')
.update(fileData)
.update(options.configString)
.digest('hex')
}
const process = (sourceText) => {
const result = yaml.safeLoad(sourceText)
const json = JSON.stringify(result, undefined, '\t')
return `module.exports = ${json}`
}
const transformer = {
getCacheKey,
process,
}
module.exports = transformer

View File

@ -23,5 +23,5 @@
"noEmit": true,
"strict": true
},
"include": ["source", "test/persistence.test.ts", "dev-server.js"]
"include": ["source", "test/**/*.ts", "dev-server.js"]
}

View File

@ -11,6 +11,11 @@ const {
module.exports = {
...common,
node: {
// This seems necessary to prevent a "Module not found: 'fs'" error when
// launching mocha-webpack:
fs: 'empty',
},
module: {
rules: [...commonLoaders(), styleLoader('style-loader')],
},

View File

@ -18,13 +18,13 @@
"@babel/preset-env": "^7.9.5",
"@babel/preset-react": "^7.9.4",
"@babel/preset-typescript": "^7.9.0",
"@jest/globals": "^27.0.6",
"@types/cheerio": "^0.22.18",
"@types/classnames": "^2.2.9",
"@types/color-convert": "^1.9.0",
"@types/iframe-resizer": "^3.5.7",
"@types/js-yaml": "^3.12.2",
"@types/mini-css-extract-plugin": "^0.9.1",
"@types/mocha": "^8.2.0",
"@types/nearley": "^2.11.1",
"@types/ramda": "^0.26.43",
"@types/raven-for-redux": "^1.1.1",
@ -47,7 +47,7 @@
"@typescript-eslint/parser": "^4.8.1",
"autoprefixer": "^9.7.6",
"babel-eslint": "^11.0.0-beta.0",
"babel-jest": "^24.9.0",
"babel-jest": "^27.0.6",
"babel-loader": "^8.0.2",
"babel-plugin-ramda": "^2.0.0",
"babel-plugin-styled-components": "^1.10.7",
@ -74,7 +74,7 @@
"intl-locales-supported": "^1.0.0",
"isomorphic-fetch": "^2.2.1",
"isomorphic-style-loader": "^5.1.0",
"jest": "^24.9.0",
"jest": "^27.0.6",
"jest-transform-nearley": "^1.0.0",
"jsdom": "^12.0.0",
"json-loader": "^0.5.7",
@ -101,7 +101,6 @@
"webpack-cli": "^3.1.2",
"webpack-dev-middleware": "^3.4.0",
"webpack-hot-middleware": "^2.24.2",
"yaml-jest": "^1.0.5",
"yaml-loader": "^0.5.0"
},
"optionalDependencies": {
@ -119,7 +118,7 @@
"lint": "yarn lint:eslintrc && yarn lint:eslint && yarn lint:prettier",
"test": "yarn workspaces run test",
"test:type": "yarn workspaces run tsc",
"test:regressions": "yarn workspace modele-social build && jest",
"test:regressions": "yarn workspace modele-social build && jest --silent",
"clean": "yarn workspaces run clean && rimraf node_modules",
"start": "concurrently -k -n publicodes,publicodes-react,mon-entreprise \"yarn workspace publicodes build:watch\" \"yarn workspace publicodes-react build --watch\" \"yarn workspace mon-entreprise start\"",
"moso:up": "yarn workspace modele-social run up && yarn workspace mon-entreprise upgrade modele-social",

1940
yarn.lock

File diff suppressed because it is too large Load Diff