From e271e7e5eab7228b0e5ce7bd4e80fd239ffa0ce7 Mon Sep 17 00:00:00 2001 From: nick-funk Date: Thu, 23 Mar 2023 11:09:25 -0600 Subject: [PATCH 1/3] separate client and server unit tests --- .github/workflows/build-and-test.yml | 7 +++-- .github/workflows/build-test-deploy.yml | 7 +++-- config/jest.client.config.js | 5 ++++ config/jest.server.config.js | 5 ++++ config/paths.client.ts | 15 +++++++++++ config/paths.server.ts | 15 +++++++++++ package.json | 5 +++- scripts/test.js | 12 +++------ scripts/testClient.js | 34 +++++++++++++++++++++++++ scripts/testServer.js | 34 +++++++++++++++++++++++++ 10 files changed, 125 insertions(+), 14 deletions(-) create mode 100644 config/jest.client.config.js create mode 100644 config/jest.server.config.js create mode 100644 config/paths.client.ts create mode 100644 config/paths.server.ts create mode 100644 scripts/testClient.js create mode 100644 scripts/testServer.js diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 9d546d34fe..772180bc0b 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -69,8 +69,11 @@ jobs: name: Lint Versions run: npx @coralproject/package-version-lint - - name: Run Unit Tests - run: npm run test -- --ci --runInBand --reporters=default --reporters=jest-junit + name: Run Server Unit Tests + run: npm run test:server -- --ci --runInBand --reporters=default --reporters=jest-junit + - + name: Run Client Unit Tests + run: npm run test:client -- --ci --runInBand --reporters=default --reporters=jest-junit - name: Build run: npm run build diff --git a/.github/workflows/build-test-deploy.yml b/.github/workflows/build-test-deploy.yml index 671a13c7d2..7faca86e81 100644 --- a/.github/workflows/build-test-deploy.yml +++ b/.github/workflows/build-test-deploy.yml @@ -84,8 +84,11 @@ jobs: name: Lint Versions run: npx @coralproject/package-version-lint - - name: Run Unit Tests - run: npm run test -- --ci --runInBand --reporters=default --reporters=jest-junit + name: Run Server Unit Tests + run: npm run test:server -- --ci --runInBand --reporters=default --reporters=jest-junit + - + name: Run Client Unit Tests + run: npm run test:client -- --ci --runInBand --reporters=default --reporters=jest-junit - name: Build run: npm run build diff --git a/config/jest.client.config.js b/config/jest.client.config.js new file mode 100644 index 0000000000..201b8fb056 --- /dev/null +++ b/config/jest.client.config.js @@ -0,0 +1,5 @@ +module.exports = { + projects: [ + "/jest/client.config.js", + ], +}; diff --git a/config/jest.server.config.js b/config/jest.server.config.js new file mode 100644 index 0000000000..c4a860b5ae --- /dev/null +++ b/config/jest.server.config.js @@ -0,0 +1,5 @@ +module.exports = { + projects: [ + "/jest/server.config.js", + ], +}; diff --git a/config/paths.client.ts b/config/paths.client.ts new file mode 100644 index 0000000000..ce533ad802 --- /dev/null +++ b/config/paths.client.ts @@ -0,0 +1,15 @@ +import fs from "fs"; +import path from "path"; +import appPaths from "../src/core/build/paths"; + +// Make sure any symlinks in the project folder are resolved: +// https://github.com/facebookincubator/create-react-app/issues/637 +const appDirectory = fs.realpathSync(process.cwd()); +const resolveApp = (relativePath: string) => + path.resolve(appDirectory, relativePath); + +// config after eject: we're in ./config/ +export default { + ...appPaths, + appJestConfig: resolveApp("config/jest.client.config.js"), +}; diff --git a/config/paths.server.ts b/config/paths.server.ts new file mode 100644 index 0000000000..10af12e2a3 --- /dev/null +++ b/config/paths.server.ts @@ -0,0 +1,15 @@ +import fs from "fs"; +import path from "path"; +import appPaths from "../src/core/build/paths"; + +// Make sure any symlinks in the project folder are resolved: +// https://github.com/facebookincubator/create-react-app/issues/637 +const appDirectory = fs.realpathSync(process.cwd()); +const resolveApp = (relativePath: string) => + path.resolve(appDirectory, relativePath); + +// config after eject: we're in ./config/ +export default { + ...appPaths, + appJestConfig: resolveApp("config/jest.server.config.js"), +}; diff --git a/package.json b/package.json index 53288274b7..9830afcb13 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,10 @@ "start:development:withJobs": "NODE_ENV=development TS_NODE_PROJECT=./src/tsconfig.json ts-node-dev --inspect --transpile-only --no-notify -r tsconfig-paths/register --ignore-watch ./docs/ ./src/index.ts", "start:webpackDevServer": "ts-node --transpile-only ./scripts/start.ts", "start": "NODE_ENV=production node dist/index.js", - "test": "node --trace-warnings scripts/test.js --env=jsdom", + "test": "node --trace-warnings scripts/test.js --watch", + "test:ci": "npm run test:server && npm run test:client", + "test:server": "node --trace-warnings scripts/testServer.js", + "test:client": "node --trace-warnings scripts/testClient.js", "tscheck:client": "tsc --project ./src/core/client/tsconfig.json --noEmit", "tscheck:scripts": "tsc --project ./tsconfig.json --noEmit", "tscheck:server": "tsc --project ./src/tsconfig.json --noEmit", diff --git a/scripts/test.js b/scripts/test.js index 08e72d4251..4ac01e7e2f 100644 --- a/scripts/test.js +++ b/scripts/test.js @@ -24,16 +24,10 @@ process.on("unhandledRejection", (err) => { const paths = require("../config/paths.ts").default; const jest = require("jest"); + const argv = process.argv.slice(2); -argv.push("--config", paths.appJestConfig); -// Watch unless on CI or in coverage mode -if ( - !process.env.CI && // ensure that the ci env var is not set. - argv.indexOf("--ci") < 0 && // ensure that the ci flag is not passed - argv.indexOf("--coverage") < 0 // ensure that the coverage flag is not passed -) { - argv.push("--watch"); -} +argv.push("--env=jsdom"); +argv.push("--config", paths.appJestConfig); jest.run(argv); diff --git a/scripts/testClient.js b/scripts/testClient.js new file mode 100644 index 0000000000..dde38e2b89 --- /dev/null +++ b/scripts/testClient.js @@ -0,0 +1,34 @@ +#!/usr/bin/env node + +"use strict"; +// Set timezone to UTC for stable tests. +process.env.TZ = "UTC"; + +// Allow importing typescript files. +require("ts-node/register"); + +// Apply all the configuration provided in the .env file. +require("dotenv").config(); + +// Do this as the first thing so that any code reading it knows the right env. +process.env.BABEL_ENV = "test"; +process.env.NODE_ENV = "test"; + +// Makes the script crash on unhandled rejections instead of silently +// ignoring them. In the future, promise rejections that are not handled will +// terminate the Node.js process with a non-zero exit code. +process.on("unhandledRejection", (err) => { + throw err; +}); + + + +const paths = require("../config/paths.client.ts").default; + +const jest = require("jest"); + +const argv = []; +argv.push("--env=jsdom"); +argv.push("--config", paths.appJestConfig); + +jest.run(argv); diff --git a/scripts/testServer.js b/scripts/testServer.js new file mode 100644 index 0000000000..6c6be422e3 --- /dev/null +++ b/scripts/testServer.js @@ -0,0 +1,34 @@ +#!/usr/bin/env node + +"use strict"; +// Set timezone to UTC for stable tests. +process.env.TZ = "UTC"; + +// Allow importing typescript files. +require("ts-node/register"); + +// Apply all the configuration provided in the .env file. +require("dotenv").config(); + +// Do this as the first thing so that any code reading it knows the right env. +process.env.BABEL_ENV = "test"; +process.env.NODE_ENV = "test"; + +// Makes the script crash on unhandled rejections instead of silently +// ignoring them. In the future, promise rejections that are not handled will +// terminate the Node.js process with a non-zero exit code. +process.on("unhandledRejection", (err) => { + throw err; +}); + + + +const paths = require("../config/paths.server.ts").default; + +const jest = require("jest"); + +const argv = []; +argv.push("--env=jsdom"); +argv.push("--config", paths.appJestConfig); + +jest.run(argv); From e2573eb0a0da780576228d70e8fb15261b09b79d Mon Sep 17 00:00:00 2001 From: nick-funk Date: Thu, 23 Mar 2023 11:38:20 -0600 Subject: [PATCH 2/3] separate client tests into stream, admin, and other for GHA --- .github/workflows/build-and-test.yml | 10 +- .github/workflows/build-test-deploy.yml | 10 +- config/jest.client.admin.config.js | 5 + config/jest.client.other.config.js | 5 + config/jest.client.stream.config.js | 5 + config/jest/client.admin.config.js | 83 +++++++++++++++ config/jest/client.other.config.js | 100 ++++++++++++++++++ config/jest/client.stream.config.js | 83 +++++++++++++++ config/paths.client.admin.ts | 15 +++ config/paths.client.other.ts | 15 +++ config/paths.client.stream.ts | 15 +++ package.json | 5 +- .../{testClient.js => testClient.admin.js} | 4 +- scripts/testClient.other.js | 32 ++++++ scripts/testClient.stream.js | 32 ++++++ scripts/testServer.js | 2 - 16 files changed, 410 insertions(+), 11 deletions(-) create mode 100644 config/jest.client.admin.config.js create mode 100644 config/jest.client.other.config.js create mode 100644 config/jest.client.stream.config.js create mode 100644 config/jest/client.admin.config.js create mode 100644 config/jest/client.other.config.js create mode 100644 config/jest/client.stream.config.js create mode 100644 config/paths.client.admin.ts create mode 100644 config/paths.client.other.ts create mode 100644 config/paths.client.stream.ts rename scripts/{testClient.js => testClient.admin.js} (92%) create mode 100644 scripts/testClient.other.js create mode 100644 scripts/testClient.stream.js diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 772180bc0b..fafd86685c 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -72,8 +72,14 @@ jobs: name: Run Server Unit Tests run: npm run test:server -- --ci --runInBand --reporters=default --reporters=jest-junit - - name: Run Client Unit Tests - run: npm run test:client -- --ci --runInBand --reporters=default --reporters=jest-junit + name: Run Client Stream Unit Tests + run: npm run test:client:stream -- --ci --runInBand --reporters=default --reporters=jest-junit + - + name: Run Client Admin Unit Tests + run: npm run test:client:admin -- --ci --runInBand --reporters=default --reporters=jest-junit + - + name: Run Client Other Unit Tests + run: npm run test:client:other -- --ci --runInBand --reporters=default --reporters=jest-junit - name: Build run: npm run build diff --git a/.github/workflows/build-test-deploy.yml b/.github/workflows/build-test-deploy.yml index 7faca86e81..d94959b390 100644 --- a/.github/workflows/build-test-deploy.yml +++ b/.github/workflows/build-test-deploy.yml @@ -87,8 +87,14 @@ jobs: name: Run Server Unit Tests run: npm run test:server -- --ci --runInBand --reporters=default --reporters=jest-junit - - name: Run Client Unit Tests - run: npm run test:client -- --ci --runInBand --reporters=default --reporters=jest-junit + name: Run Client Stream Unit Tests + run: npm run test:client:stream -- --ci --runInBand --reporters=default --reporters=jest-junit + - + name: Run Client Admin Unit Tests + run: npm run test:client:admin -- --ci --runInBand --reporters=default --reporters=jest-junit + - + name: Run Client Other Unit Tests + run: npm run test:client:other -- --ci --runInBand --reporters=default --reporters=jest-junit - name: Build run: npm run build diff --git a/config/jest.client.admin.config.js b/config/jest.client.admin.config.js new file mode 100644 index 0000000000..b974494e66 --- /dev/null +++ b/config/jest.client.admin.config.js @@ -0,0 +1,5 @@ +module.exports = { + projects: [ + "/jest/client.admin.config.js", + ], +}; diff --git a/config/jest.client.other.config.js b/config/jest.client.other.config.js new file mode 100644 index 0000000000..2155bc8f7b --- /dev/null +++ b/config/jest.client.other.config.js @@ -0,0 +1,5 @@ +module.exports = { + projects: [ + "/jest/client.other.config.js", + ], +}; diff --git a/config/jest.client.stream.config.js b/config/jest.client.stream.config.js new file mode 100644 index 0000000000..da30f125e7 --- /dev/null +++ b/config/jest.client.stream.config.js @@ -0,0 +1,5 @@ +module.exports = { + projects: [ + "/jest/client.stream.config.js", + ], +}; diff --git a/config/jest/client.admin.config.js b/config/jest/client.admin.config.js new file mode 100644 index 0000000000..7b42316637 --- /dev/null +++ b/config/jest/client.admin.config.js @@ -0,0 +1,83 @@ +const path = require("path"); + +const d3Pkgs = [ + "d3", + "d3-array", + "d3-axis", + "d3-brush", + "d3-chord", + "d3-color", + "d3-contour", + "d3-delaunay", + "d3-dispatch", + "d3-drag", + "d3-dsv", + "d3-ease", + "d3-fetch", + "d3-force", + "d3-format", + "d3-geo", + "d3-hierarchy", + "d3-interpolate", + "d3-path", + "d3-polygon", + "d3-quadtree", + "d3-random", + "d3-scale", + "d3-scale-chromatic", + "d3-selection", + "d3-shape", + "d3-time", + "d3-time-format", + "d3-timer", + "d3-transition", + "d3-zoom", +]; + +module.exports = { + displayName: "client", + rootDir: "../../", + roots: ["/src/core/client/admin"], + collectCoverageFrom: ["**/*.{js,jsx,mjs,ts,tsx}"], + coveragePathIgnorePatterns: ["/node_modules/"], + setupFiles: [ + "/src/core/client/test/polyfills.ts", + "/src/core/client/test/setup.ts", + ], + setupFilesAfterEnv: ["/src/core/client/test/setupTestFramework.ts"], + testMatch: ["**/*.spec.{js,jsx,mjs,ts,tsx}"], + testEnvironment: "node", + testURL: "http://localhost", + transform: { + "^.+\\.jsx?$": "/node_modules/babel-jest", + "^.+\\.tsx?$": "/node_modules/ts-jest", + "^.+\\.css$": "/config/jest/cssTransform.js", + "^.+\\.ftl$": "/config/jest/contentTransform.js", + "^(?!.*\\.(js|jsx|mjs|ts|tsx|css|json|ftl)$)": + "/config/jest/fileTransform.js", + }, + transformIgnorePatterns: [ + `[/\\\\]node_modules[/\\\\](?!(fluent|react-relay-network-modern|@coralproject/rte/lib|internmap|${d3Pkgs.join( + "|" + )})[/\\\\]).+\\.(js|jsx|mjs|ts|tsx)$`, + ], + moduleNameMapper: { + "^coral-account/(.*)$": "/src/core/client/account/$1", + "^coral-admin/(.*)$": "/src/core/client/admin/$1", + "^coral-auth/(.*)$": "/src/core/client/auth/$1", + "^coral-count/(.*)$": "/src/core/client/count/$1", + "^coral-ui/(.*)$": "/src/core/client/ui/$1", + "^coral-stream/(.*)$": "/src/core/client/stream/$1", + "^coral-framework/(.*)$": "/src/core/client/framework/$1", + "^coral-common/(.*)$": "/src/core/common/$1", + "^coral-test/(.*)$": "/src/core/client/test/$1", + }, + moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "ftl"], + globals: { + "ts-jest": { + babelConfig: true, + tsConfig: path.resolve(__dirname, "../../src/core/client/tsconfig.json"), + }, + }, + preset: "ts-jest/presets/js-with-babel", +}; diff --git a/config/jest/client.other.config.js b/config/jest/client.other.config.js new file mode 100644 index 0000000000..67cdc860cc --- /dev/null +++ b/config/jest/client.other.config.js @@ -0,0 +1,100 @@ +const fs = require("fs"); +const path = require("path"); + +const d3Pkgs = [ + "d3", + "d3-array", + "d3-axis", + "d3-brush", + "d3-chord", + "d3-color", + "d3-contour", + "d3-delaunay", + "d3-dispatch", + "d3-drag", + "d3-dsv", + "d3-ease", + "d3-fetch", + "d3-force", + "d3-format", + "d3-geo", + "d3-hierarchy", + "d3-interpolate", + "d3-path", + "d3-polygon", + "d3-quadtree", + "d3-random", + "d3-scale", + "d3-scale-chromatic", + "d3-selection", + "d3-shape", + "d3-time", + "d3-time-format", + "d3-timer", + "d3-transition", + "d3-zoom", +]; + +const dir = "./src/core/client"; +const dirItems = fs.readdirSync(dir); +let roots = []; + +const excludedRoots = ["src/core/client/stream", "src/core/client/admin"]; + +for (const item of dirItems) { + const fullPath = path.join(dir, item); + const stats = fs.statSync(fullPath); + if (stats.isDirectory() && !excludedRoots.includes(fullPath)) { + roots.push(fullPath); + } +} + +roots = roots.map((r) => `/${r}`); + +module.exports = { + displayName: "client", + rootDir: "../../", + roots, + collectCoverageFrom: ["**/*.{js,jsx,mjs,ts,tsx}"], + coveragePathIgnorePatterns: ["/node_modules/"], + setupFiles: [ + "/src/core/client/test/polyfills.ts", + "/src/core/client/test/setup.ts", + ], + setupFilesAfterEnv: ["/src/core/client/test/setupTestFramework.ts"], + testMatch: ["**/*.spec.{js,jsx,mjs,ts,tsx}"], + testEnvironment: "node", + testURL: "http://localhost", + transform: { + "^.+\\.jsx?$": "/node_modules/babel-jest", + "^.+\\.tsx?$": "/node_modules/ts-jest", + "^.+\\.css$": "/config/jest/cssTransform.js", + "^.+\\.ftl$": "/config/jest/contentTransform.js", + "^(?!.*\\.(js|jsx|mjs|ts|tsx|css|json|ftl)$)": + "/config/jest/fileTransform.js", + }, + transformIgnorePatterns: [ + `[/\\\\]node_modules[/\\\\](?!(fluent|react-relay-network-modern|@coralproject/rte/lib|internmap|${d3Pkgs.join( + "|" + )})[/\\\\]).+\\.(js|jsx|mjs|ts|tsx)$`, + ], + moduleNameMapper: { + "^coral-account/(.*)$": "/src/core/client/account/$1", + "^coral-admin/(.*)$": "/src/core/client/admin/$1", + "^coral-auth/(.*)$": "/src/core/client/auth/$1", + "^coral-count/(.*)$": "/src/core/client/count/$1", + "^coral-ui/(.*)$": "/src/core/client/ui/$1", + "^coral-stream/(.*)$": "/src/core/client/stream/$1", + "^coral-framework/(.*)$": "/src/core/client/framework/$1", + "^coral-common/(.*)$": "/src/core/common/$1", + "^coral-test/(.*)$": "/src/core/client/test/$1", + }, + moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "ftl"], + globals: { + "ts-jest": { + babelConfig: true, + tsConfig: path.resolve(__dirname, "../../src/core/client/tsconfig.json"), + }, + }, + preset: "ts-jest/presets/js-with-babel", +}; diff --git a/config/jest/client.stream.config.js b/config/jest/client.stream.config.js new file mode 100644 index 0000000000..b765736273 --- /dev/null +++ b/config/jest/client.stream.config.js @@ -0,0 +1,83 @@ +const path = require("path"); + +const d3Pkgs = [ + "d3", + "d3-array", + "d3-axis", + "d3-brush", + "d3-chord", + "d3-color", + "d3-contour", + "d3-delaunay", + "d3-dispatch", + "d3-drag", + "d3-dsv", + "d3-ease", + "d3-fetch", + "d3-force", + "d3-format", + "d3-geo", + "d3-hierarchy", + "d3-interpolate", + "d3-path", + "d3-polygon", + "d3-quadtree", + "d3-random", + "d3-scale", + "d3-scale-chromatic", + "d3-selection", + "d3-shape", + "d3-time", + "d3-time-format", + "d3-timer", + "d3-transition", + "d3-zoom", +]; + +module.exports = { + displayName: "client:stream", + rootDir: "../../", + roots: ["/src/core/client/stream"], + collectCoverageFrom: ["**/*.{js,jsx,mjs,ts,tsx}"], + coveragePathIgnorePatterns: ["/node_modules/"], + setupFiles: [ + "/src/core/client/test/polyfills.ts", + "/src/core/client/test/setup.ts", + ], + setupFilesAfterEnv: ["/src/core/client/test/setupTestFramework.ts"], + testMatch: ["**/*.spec.{js,jsx,mjs,ts,tsx}"], + testEnvironment: "node", + testURL: "http://localhost", + transform: { + "^.+\\.jsx?$": "/node_modules/babel-jest", + "^.+\\.tsx?$": "/node_modules/ts-jest", + "^.+\\.css$": "/config/jest/cssTransform.js", + "^.+\\.ftl$": "/config/jest/contentTransform.js", + "^(?!.*\\.(js|jsx|mjs|ts|tsx|css|json|ftl)$)": + "/config/jest/fileTransform.js", + }, + transformIgnorePatterns: [ + `[/\\\\]node_modules[/\\\\](?!(fluent|react-relay-network-modern|@coralproject/rte/lib|internmap|${d3Pkgs.join( + "|" + )})[/\\\\]).+\\.(js|jsx|mjs|ts|tsx)$`, + ], + moduleNameMapper: { + "^coral-account/(.*)$": "/src/core/client/account/$1", + "^coral-admin/(.*)$": "/src/core/client/admin/$1", + "^coral-auth/(.*)$": "/src/core/client/auth/$1", + "^coral-count/(.*)$": "/src/core/client/count/$1", + "^coral-ui/(.*)$": "/src/core/client/ui/$1", + "^coral-stream/(.*)$": "/src/core/client/stream/$1", + "^coral-framework/(.*)$": "/src/core/client/framework/$1", + "^coral-common/(.*)$": "/src/core/common/$1", + "^coral-test/(.*)$": "/src/core/client/test/$1", + }, + moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "ftl"], + globals: { + "ts-jest": { + babelConfig: true, + tsConfig: path.resolve(__dirname, "../../src/core/client/tsconfig.json"), + }, + }, + preset: "ts-jest/presets/js-with-babel", +}; diff --git a/config/paths.client.admin.ts b/config/paths.client.admin.ts new file mode 100644 index 0000000000..a0fecc2294 --- /dev/null +++ b/config/paths.client.admin.ts @@ -0,0 +1,15 @@ +import fs from "fs"; +import path from "path"; +import appPaths from "../src/core/build/paths"; + +// Make sure any symlinks in the project folder are resolved: +// https://github.com/facebookincubator/create-react-app/issues/637 +const appDirectory = fs.realpathSync(process.cwd()); +const resolveApp = (relativePath: string) => + path.resolve(appDirectory, relativePath); + +// config after eject: we're in ./config/ +export default { + ...appPaths, + appJestConfig: resolveApp("config/jest.client.admin.config.js"), +}; diff --git a/config/paths.client.other.ts b/config/paths.client.other.ts new file mode 100644 index 0000000000..f2ddd1ef91 --- /dev/null +++ b/config/paths.client.other.ts @@ -0,0 +1,15 @@ +import fs from "fs"; +import path from "path"; +import appPaths from "../src/core/build/paths"; + +// Make sure any symlinks in the project folder are resolved: +// https://github.com/facebookincubator/create-react-app/issues/637 +const appDirectory = fs.realpathSync(process.cwd()); +const resolveApp = (relativePath: string) => + path.resolve(appDirectory, relativePath); + +// config after eject: we're in ./config/ +export default { + ...appPaths, + appJestConfig: resolveApp("config/jest.client.other.config.js"), +}; diff --git a/config/paths.client.stream.ts b/config/paths.client.stream.ts new file mode 100644 index 0000000000..01001b9846 --- /dev/null +++ b/config/paths.client.stream.ts @@ -0,0 +1,15 @@ +import fs from "fs"; +import path from "path"; +import appPaths from "../src/core/build/paths"; + +// Make sure any symlinks in the project folder are resolved: +// https://github.com/facebookincubator/create-react-app/issues/637 +const appDirectory = fs.realpathSync(process.cwd()); +const resolveApp = (relativePath: string) => + path.resolve(appDirectory, relativePath); + +// config after eject: we're in ./config/ +export default { + ...appPaths, + appJestConfig: resolveApp("config/jest.client.stream.config.js"), +}; diff --git a/package.json b/package.json index 9830afcb13..34d33a09b3 100644 --- a/package.json +++ b/package.json @@ -58,9 +58,10 @@ "start:webpackDevServer": "ts-node --transpile-only ./scripts/start.ts", "start": "NODE_ENV=production node dist/index.js", "test": "node --trace-warnings scripts/test.js --watch", - "test:ci": "npm run test:server && npm run test:client", "test:server": "node --trace-warnings scripts/testServer.js", - "test:client": "node --trace-warnings scripts/testClient.js", + "test:client:stream": "node --trace-warnings scripts/testClient.stream.js", + "test:client:admin": "node --trace-warnings scripts/testClient.admin.js", + "test:client:other": "node --trace-warnings scripts/testClient.other.js", "tscheck:client": "tsc --project ./src/core/client/tsconfig.json --noEmit", "tscheck:scripts": "tsc --project ./tsconfig.json --noEmit", "tscheck:server": "tsc --project ./src/tsconfig.json --noEmit", diff --git a/scripts/testClient.js b/scripts/testClient.admin.js similarity index 92% rename from scripts/testClient.js rename to scripts/testClient.admin.js index dde38e2b89..98947fb69f 100644 --- a/scripts/testClient.js +++ b/scripts/testClient.admin.js @@ -21,9 +21,7 @@ process.on("unhandledRejection", (err) => { throw err; }); - - -const paths = require("../config/paths.client.ts").default; +const paths = require("../config/paths.client.admin.ts").default; const jest = require("jest"); diff --git a/scripts/testClient.other.js b/scripts/testClient.other.js new file mode 100644 index 0000000000..3c713eba55 --- /dev/null +++ b/scripts/testClient.other.js @@ -0,0 +1,32 @@ +#!/usr/bin/env node + +"use strict"; +// Set timezone to UTC for stable tests. +process.env.TZ = "UTC"; + +// Allow importing typescript files. +require("ts-node/register"); + +// Apply all the configuration provided in the .env file. +require("dotenv").config(); + +// Do this as the first thing so that any code reading it knows the right env. +process.env.BABEL_ENV = "test"; +process.env.NODE_ENV = "test"; + +// Makes the script crash on unhandled rejections instead of silently +// ignoring them. In the future, promise rejections that are not handled will +// terminate the Node.js process with a non-zero exit code. +process.on("unhandledRejection", (err) => { + throw err; +}); + +const paths = require("../config/paths.client.other.ts").default; + +const jest = require("jest"); + +const argv = []; +argv.push("--env=jsdom"); +argv.push("--config", paths.appJestConfig); + +jest.run(argv); diff --git a/scripts/testClient.stream.js b/scripts/testClient.stream.js new file mode 100644 index 0000000000..4bc951dc37 --- /dev/null +++ b/scripts/testClient.stream.js @@ -0,0 +1,32 @@ +#!/usr/bin/env node + +"use strict"; +// Set timezone to UTC for stable tests. +process.env.TZ = "UTC"; + +// Allow importing typescript files. +require("ts-node/register"); + +// Apply all the configuration provided in the .env file. +require("dotenv").config(); + +// Do this as the first thing so that any code reading it knows the right env. +process.env.BABEL_ENV = "test"; +process.env.NODE_ENV = "test"; + +// Makes the script crash on unhandled rejections instead of silently +// ignoring them. In the future, promise rejections that are not handled will +// terminate the Node.js process with a non-zero exit code. +process.on("unhandledRejection", (err) => { + throw err; +}); + +const paths = require("../config/paths.client.stream.ts").default; + +const jest = require("jest"); + +const argv = []; +argv.push("--env=jsdom"); +argv.push("--config", paths.appJestConfig); + +jest.run(argv); diff --git a/scripts/testServer.js b/scripts/testServer.js index 6c6be422e3..f97acb8042 100644 --- a/scripts/testServer.js +++ b/scripts/testServer.js @@ -21,8 +21,6 @@ process.on("unhandledRejection", (err) => { throw err; }); - - const paths = require("../config/paths.server.ts").default; const jest = require("jest"); From 27fd43a8bf5c21b44a8d23691929f21cf53c3c5d Mon Sep 17 00:00:00 2001 From: nick-funk Date: Fri, 24 Mar 2023 10:45:13 -0600 Subject: [PATCH 3/3] refactor client jest configs to re-use base config --- config/jest/client.admin.config.js | 84 ++-------------------------- config/jest/client.baseConfig.js | 87 +++++++++++++++++++++++++++++ config/jest/client.config.js | 82 ++------------------------- config/jest/client.other.config.js | 87 +++-------------------------- config/jest/client.stream.config.js | 82 ++------------------------- 5 files changed, 107 insertions(+), 315 deletions(-) create mode 100644 config/jest/client.baseConfig.js diff --git a/config/jest/client.admin.config.js b/config/jest/client.admin.config.js index 7b42316637..581c9d06e1 100644 --- a/config/jest/client.admin.config.js +++ b/config/jest/client.admin.config.js @@ -1,83 +1,9 @@ -const path = require("path"); +const createConfig = require("./client.baseConfig"); -const d3Pkgs = [ - "d3", - "d3-array", - "d3-axis", - "d3-brush", - "d3-chord", - "d3-color", - "d3-contour", - "d3-delaunay", - "d3-dispatch", - "d3-drag", - "d3-dsv", - "d3-ease", - "d3-fetch", - "d3-force", - "d3-format", - "d3-geo", - "d3-hierarchy", - "d3-interpolate", - "d3-path", - "d3-polygon", - "d3-quadtree", - "d3-random", - "d3-scale", - "d3-scale-chromatic", - "d3-selection", - "d3-shape", - "d3-time", - "d3-time-format", - "d3-timer", - "d3-transition", - "d3-zoom", -]; +const baseConfig = createConfig(); module.exports = { - displayName: "client", - rootDir: "../../", + ...baseConfig, + displayName: "client:admin", roots: ["/src/core/client/admin"], - collectCoverageFrom: ["**/*.{js,jsx,mjs,ts,tsx}"], - coveragePathIgnorePatterns: ["/node_modules/"], - setupFiles: [ - "/src/core/client/test/polyfills.ts", - "/src/core/client/test/setup.ts", - ], - setupFilesAfterEnv: ["/src/core/client/test/setupTestFramework.ts"], - testMatch: ["**/*.spec.{js,jsx,mjs,ts,tsx}"], - testEnvironment: "node", - testURL: "http://localhost", - transform: { - "^.+\\.jsx?$": "/node_modules/babel-jest", - "^.+\\.tsx?$": "/node_modules/ts-jest", - "^.+\\.css$": "/config/jest/cssTransform.js", - "^.+\\.ftl$": "/config/jest/contentTransform.js", - "^(?!.*\\.(js|jsx|mjs|ts|tsx|css|json|ftl)$)": - "/config/jest/fileTransform.js", - }, - transformIgnorePatterns: [ - `[/\\\\]node_modules[/\\\\](?!(fluent|react-relay-network-modern|@coralproject/rte/lib|internmap|${d3Pkgs.join( - "|" - )})[/\\\\]).+\\.(js|jsx|mjs|ts|tsx)$`, - ], - moduleNameMapper: { - "^coral-account/(.*)$": "/src/core/client/account/$1", - "^coral-admin/(.*)$": "/src/core/client/admin/$1", - "^coral-auth/(.*)$": "/src/core/client/auth/$1", - "^coral-count/(.*)$": "/src/core/client/count/$1", - "^coral-ui/(.*)$": "/src/core/client/ui/$1", - "^coral-stream/(.*)$": "/src/core/client/stream/$1", - "^coral-framework/(.*)$": "/src/core/client/framework/$1", - "^coral-common/(.*)$": "/src/core/common/$1", - "^coral-test/(.*)$": "/src/core/client/test/$1", - }, - moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "ftl"], - globals: { - "ts-jest": { - babelConfig: true, - tsConfig: path.resolve(__dirname, "../../src/core/client/tsconfig.json"), - }, - }, - preset: "ts-jest/presets/js-with-babel", -}; +} diff --git a/config/jest/client.baseConfig.js b/config/jest/client.baseConfig.js new file mode 100644 index 0000000000..aa55488524 --- /dev/null +++ b/config/jest/client.baseConfig.js @@ -0,0 +1,87 @@ +const path = require("path"); + +const createConfig = () => { + const d3Pkgs = [ + "d3", + "d3-array", + "d3-axis", + "d3-brush", + "d3-chord", + "d3-color", + "d3-contour", + "d3-delaunay", + "d3-dispatch", + "d3-drag", + "d3-dsv", + "d3-ease", + "d3-fetch", + "d3-force", + "d3-format", + "d3-geo", + "d3-hierarchy", + "d3-interpolate", + "d3-path", + "d3-polygon", + "d3-quadtree", + "d3-random", + "d3-scale", + "d3-scale-chromatic", + "d3-selection", + "d3-shape", + "d3-time", + "d3-time-format", + "d3-timer", + "d3-transition", + "d3-zoom", + ]; + + return { + displayName: "jestBaseConfig", + rootDir: "../../", + roots: ["/src/core/"], + collectCoverageFrom: ["**/*.{js,jsx,mjs,ts,tsx}"], + coveragePathIgnorePatterns: ["/node_modules/"], + setupFiles: [ + "/src/core/client/test/polyfills.ts", + "/src/core/client/test/setup.ts", + ], + setupFilesAfterEnv: ["/src/core/client/test/setupTestFramework.ts"], + testMatch: ["**/*.spec.{js,jsx,mjs,ts,tsx}"], + testEnvironment: "node", + testURL: "http://localhost", + transform: { + "^.+\\.jsx?$": "/node_modules/babel-jest", + "^.+\\.tsx?$": "/node_modules/ts-jest", + "^.+\\.css$": "/config/jest/cssTransform.js", + "^.+\\.ftl$": "/config/jest/contentTransform.js", + "^(?!.*\\.(js|jsx|mjs|ts|tsx|css|json|ftl)$)": + "/config/jest/fileTransform.js", + }, + transformIgnorePatterns: [ + `[/\\\\]node_modules[/\\\\](?!(fluent|react-relay-network-modern|@coralproject/rte/lib|internmap|${d3Pkgs.join( + "|" + )})[/\\\\]).+\\.(js|jsx|mjs|ts|tsx)$`, + ], + moduleNameMapper: { + "^coral-account/(.*)$": "/src/core/client/account/$1", + "^coral-admin/(.*)$": "/src/core/client/admin/$1", + "^coral-auth/(.*)$": "/src/core/client/auth/$1", + "^coral-count/(.*)$": "/src/core/client/count/$1", + "^coral-ui/(.*)$": "/src/core/client/ui/$1", + "^coral-stream/(.*)$": "/src/core/client/stream/$1", + "^coral-framework/(.*)$": "/src/core/client/framework/$1", + "^coral-common/(.*)$": "/src/core/common/$1", + "^coral-test/(.*)$": "/src/core/client/test/$1", + }, + moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "ftl"], + globals: { + "ts-jest": { + babelConfig: true, + tsConfig: path.resolve(__dirname, "../../src/core/client/tsconfig.json"), + }, + }, + preset: "ts-jest/presets/js-with-babel", + }; +} + +module.exports = createConfig; diff --git a/config/jest/client.config.js b/config/jest/client.config.js index 9deeb0544e..55fe7b40ef 100644 --- a/config/jest/client.config.js +++ b/config/jest/client.config.js @@ -1,83 +1,9 @@ -const path = require("path"); +const createConfig = require("./client.baseConfig"); -const d3Pkgs = [ - "d3", - "d3-array", - "d3-axis", - "d3-brush", - "d3-chord", - "d3-color", - "d3-contour", - "d3-delaunay", - "d3-dispatch", - "d3-drag", - "d3-dsv", - "d3-ease", - "d3-fetch", - "d3-force", - "d3-format", - "d3-geo", - "d3-hierarchy", - "d3-interpolate", - "d3-path", - "d3-polygon", - "d3-quadtree", - "d3-random", - "d3-scale", - "d3-scale-chromatic", - "d3-selection", - "d3-shape", - "d3-time", - "d3-time-format", - "d3-timer", - "d3-transition", - "d3-zoom", -]; +const baseConfig = createConfig(); module.exports = { + ...baseConfig, displayName: "client", - rootDir: "../../", roots: ["/src/core/client"], - collectCoverageFrom: ["**/*.{js,jsx,mjs,ts,tsx}"], - coveragePathIgnorePatterns: ["/node_modules/"], - setupFiles: [ - "/src/core/client/test/polyfills.ts", - "/src/core/client/test/setup.ts", - ], - setupFilesAfterEnv: ["/src/core/client/test/setupTestFramework.ts"], - testMatch: ["**/*.spec.{js,jsx,mjs,ts,tsx}"], - testEnvironment: "node", - testURL: "http://localhost", - transform: { - "^.+\\.jsx?$": "/node_modules/babel-jest", - "^.+\\.tsx?$": "/node_modules/ts-jest", - "^.+\\.css$": "/config/jest/cssTransform.js", - "^.+\\.ftl$": "/config/jest/contentTransform.js", - "^(?!.*\\.(js|jsx|mjs|ts|tsx|css|json|ftl)$)": - "/config/jest/fileTransform.js", - }, - transformIgnorePatterns: [ - `[/\\\\]node_modules[/\\\\](?!(fluent|react-relay-network-modern|@coralproject/rte/lib|internmap|${d3Pkgs.join( - "|" - )})[/\\\\]).+\\.(js|jsx|mjs|ts|tsx)$`, - ], - moduleNameMapper: { - "^coral-account/(.*)$": "/src/core/client/account/$1", - "^coral-admin/(.*)$": "/src/core/client/admin/$1", - "^coral-auth/(.*)$": "/src/core/client/auth/$1", - "^coral-count/(.*)$": "/src/core/client/count/$1", - "^coral-ui/(.*)$": "/src/core/client/ui/$1", - "^coral-stream/(.*)$": "/src/core/client/stream/$1", - "^coral-framework/(.*)$": "/src/core/client/framework/$1", - "^coral-common/(.*)$": "/src/core/common/$1", - "^coral-test/(.*)$": "/src/core/client/test/$1", - }, - moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "ftl"], - globals: { - "ts-jest": { - babelConfig: true, - tsConfig: path.resolve(__dirname, "../../src/core/client/tsconfig.json"), - }, - }, - preset: "ts-jest/presets/js-with-babel", -}; +} diff --git a/config/jest/client.other.config.js b/config/jest/client.other.config.js index 67cdc860cc..a9dccb5e61 100644 --- a/config/jest/client.other.config.js +++ b/config/jest/client.other.config.js @@ -1,39 +1,6 @@ const fs = require("fs"); const path = require("path"); - -const d3Pkgs = [ - "d3", - "d3-array", - "d3-axis", - "d3-brush", - "d3-chord", - "d3-color", - "d3-contour", - "d3-delaunay", - "d3-dispatch", - "d3-drag", - "d3-dsv", - "d3-ease", - "d3-fetch", - "d3-force", - "d3-format", - "d3-geo", - "d3-hierarchy", - "d3-interpolate", - "d3-path", - "d3-polygon", - "d3-quadtree", - "d3-random", - "d3-scale", - "d3-scale-chromatic", - "d3-selection", - "d3-shape", - "d3-time", - "d3-time-format", - "d3-timer", - "d3-transition", - "d3-zoom", -]; +const createConfig = require("./client.baseConfig"); const dir = "./src/core/client"; const dirItems = fs.readdirSync(dir); @@ -51,50 +18,10 @@ for (const item of dirItems) { roots = roots.map((r) => `/${r}`); +const baseConfig = createConfig(); + module.exports = { - displayName: "client", - rootDir: "../../", - roots, - collectCoverageFrom: ["**/*.{js,jsx,mjs,ts,tsx}"], - coveragePathIgnorePatterns: ["/node_modules/"], - setupFiles: [ - "/src/core/client/test/polyfills.ts", - "/src/core/client/test/setup.ts", - ], - setupFilesAfterEnv: ["/src/core/client/test/setupTestFramework.ts"], - testMatch: ["**/*.spec.{js,jsx,mjs,ts,tsx}"], - testEnvironment: "node", - testURL: "http://localhost", - transform: { - "^.+\\.jsx?$": "/node_modules/babel-jest", - "^.+\\.tsx?$": "/node_modules/ts-jest", - "^.+\\.css$": "/config/jest/cssTransform.js", - "^.+\\.ftl$": "/config/jest/contentTransform.js", - "^(?!.*\\.(js|jsx|mjs|ts|tsx|css|json|ftl)$)": - "/config/jest/fileTransform.js", - }, - transformIgnorePatterns: [ - `[/\\\\]node_modules[/\\\\](?!(fluent|react-relay-network-modern|@coralproject/rte/lib|internmap|${d3Pkgs.join( - "|" - )})[/\\\\]).+\\.(js|jsx|mjs|ts|tsx)$`, - ], - moduleNameMapper: { - "^coral-account/(.*)$": "/src/core/client/account/$1", - "^coral-admin/(.*)$": "/src/core/client/admin/$1", - "^coral-auth/(.*)$": "/src/core/client/auth/$1", - "^coral-count/(.*)$": "/src/core/client/count/$1", - "^coral-ui/(.*)$": "/src/core/client/ui/$1", - "^coral-stream/(.*)$": "/src/core/client/stream/$1", - "^coral-framework/(.*)$": "/src/core/client/framework/$1", - "^coral-common/(.*)$": "/src/core/common/$1", - "^coral-test/(.*)$": "/src/core/client/test/$1", - }, - moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "ftl"], - globals: { - "ts-jest": { - babelConfig: true, - tsConfig: path.resolve(__dirname, "../../src/core/client/tsconfig.json"), - }, - }, - preset: "ts-jest/presets/js-with-babel", -}; + ...baseConfig, + displayName: "client:other", + roots +} diff --git a/config/jest/client.stream.config.js b/config/jest/client.stream.config.js index b765736273..9da5f367ad 100644 --- a/config/jest/client.stream.config.js +++ b/config/jest/client.stream.config.js @@ -1,83 +1,9 @@ -const path = require("path"); +const createConfig = require("./client.baseConfig"); -const d3Pkgs = [ - "d3", - "d3-array", - "d3-axis", - "d3-brush", - "d3-chord", - "d3-color", - "d3-contour", - "d3-delaunay", - "d3-dispatch", - "d3-drag", - "d3-dsv", - "d3-ease", - "d3-fetch", - "d3-force", - "d3-format", - "d3-geo", - "d3-hierarchy", - "d3-interpolate", - "d3-path", - "d3-polygon", - "d3-quadtree", - "d3-random", - "d3-scale", - "d3-scale-chromatic", - "d3-selection", - "d3-shape", - "d3-time", - "d3-time-format", - "d3-timer", - "d3-transition", - "d3-zoom", -]; +const baseConfig = createConfig(); module.exports = { + ...baseConfig, displayName: "client:stream", - rootDir: "../../", roots: ["/src/core/client/stream"], - collectCoverageFrom: ["**/*.{js,jsx,mjs,ts,tsx}"], - coveragePathIgnorePatterns: ["/node_modules/"], - setupFiles: [ - "/src/core/client/test/polyfills.ts", - "/src/core/client/test/setup.ts", - ], - setupFilesAfterEnv: ["/src/core/client/test/setupTestFramework.ts"], - testMatch: ["**/*.spec.{js,jsx,mjs,ts,tsx}"], - testEnvironment: "node", - testURL: "http://localhost", - transform: { - "^.+\\.jsx?$": "/node_modules/babel-jest", - "^.+\\.tsx?$": "/node_modules/ts-jest", - "^.+\\.css$": "/config/jest/cssTransform.js", - "^.+\\.ftl$": "/config/jest/contentTransform.js", - "^(?!.*\\.(js|jsx|mjs|ts|tsx|css|json|ftl)$)": - "/config/jest/fileTransform.js", - }, - transformIgnorePatterns: [ - `[/\\\\]node_modules[/\\\\](?!(fluent|react-relay-network-modern|@coralproject/rte/lib|internmap|${d3Pkgs.join( - "|" - )})[/\\\\]).+\\.(js|jsx|mjs|ts|tsx)$`, - ], - moduleNameMapper: { - "^coral-account/(.*)$": "/src/core/client/account/$1", - "^coral-admin/(.*)$": "/src/core/client/admin/$1", - "^coral-auth/(.*)$": "/src/core/client/auth/$1", - "^coral-count/(.*)$": "/src/core/client/count/$1", - "^coral-ui/(.*)$": "/src/core/client/ui/$1", - "^coral-stream/(.*)$": "/src/core/client/stream/$1", - "^coral-framework/(.*)$": "/src/core/client/framework/$1", - "^coral-common/(.*)$": "/src/core/common/$1", - "^coral-test/(.*)$": "/src/core/client/test/$1", - }, - moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "ftl"], - globals: { - "ts-jest": { - babelConfig: true, - tsConfig: path.resolve(__dirname, "../../src/core/client/tsconfig.json"), - }, - }, - preset: "ts-jest/presets/js-with-babel", -}; +}