Skip to content

Commit

Permalink
Port from Ava to Jest (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp authored Dec 20, 2024
1 parent 02f0f49 commit 3b9114e
Show file tree
Hide file tree
Showing 71 changed files with 3,771 additions and 3,303 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
matrix:
# Do not support --experimental-require-module
# Commented until we have a solution to run tests without it
# node-version: [14, 16, 18, 20, 22]
# node-version: [20, 22] // fails on 20 with (0 , _getDomain.default) is not a function
# node: [14, 16, 18, 20, 22]
# node: [20, 22] // fails on 20 with (0 , _getDomain.default) is not a function
node: [22]
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ At that point you can make changes to the xmpp.js code and run tests with
make test
```

If you want to iterate faster, you can watch a test file with `npx ava --watch packages/debug/test.js`.
If you want to iterate faster, you can watch a test file with `npx jest --watch packages/debug/test.js`.

See [ava CLI](https://github.com/avajs/ava/blob/main/docs/05-command-line.md).
See [Jest CLI](https://jestjs.io/docs/cli).

## Submitting

Expand Down
19 changes: 11 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: setup lint test ci clean start stop restart bundlesize bundle size cert ncu
.PHONY: setup lint test ci clean start stop restart bundlesize bundle size ncu

setup:
node packages/xmpp.js/script.js
Expand All @@ -7,34 +7,37 @@ setup:
node bundle.js

lint:
./node_modules/.bin/eslint --cache .
npx eslint --cache .

test:
cd packages/xmpp.js/ && npm run prepublish
npm install
node bundle.js
./node_modules/.bin/ava
npx jest
make lint
make bundlesize

ci:
npm install
./node_modules/.bin/ava
npx jest
make lint
make restart
./node_modules/.bin/lerna run prepublish
npx lerna run prepublish
node bundle.js
./node_modules/.bin/ava --config e2e.config.js
make e2e
make bundlesize

e2e:
NODE_TLS_REJECT_UNAUTHORIZED=0 npx jest --runInBand --config e2e.config.js

clean:
make stop
rm -f server/localhost.key
rm -f server/localhost.crt
rm -f server/prosody.err
rm -f server/prosody.log
rm -f server/prosody.pid
./node_modules/.bin/lerna clean --yes
npx lerna clean --yes
rm -rf node_modules/
rm -f packages/*/dist/*.js
rm -f lerna-debug.log
Expand All @@ -49,7 +52,7 @@ restart:
./server/ctl.js restart

bundlesize:
./node_modules/.bin/bundlesize
npx bundlesize

bundle:
node bundle.js
Expand Down
30 changes: 0 additions & 30 deletions ava.config.js

This file was deleted.

56 changes: 41 additions & 15 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
"use strict";

module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
ie: "10",
module.exports = function config(api) {
const isTest = api.env("test");

if (isTest) {
return {
plugins: [
[
"@babel/plugin-transform-react-jsx",
{
pragma: "xml",
throwIfNamespace: false,
},
],
[
"babel-plugin-jsx-pragmatic",
{
module: "@xmpp/xml",
import: "xml",
},
],
"@babel/plugin-transform-modules-commonjs",
],
};
}

return {
presets: [
[
"@babel/preset-env",
{
targets: {
ie: "10",
},
loose: true,
},
loose: true,
},
],
],
plugins: [
"@babel/plugin-transform-runtime",
"babel-plugin-transform-async-to-promises",
"@babel/plugin-proposal-object-rest-spread",
],
],
plugins: [
"@babel/plugin-transform-runtime",
"babel-plugin-transform-async-to-promises",
"@babel/plugin-proposal-object-rest-spread",
],
};
};
4 changes: 1 addition & 3 deletions bundle.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env node

/* eslint-disable no-console */

'use strict' // eslint-disable-line node/shebang
'use strict'

const fs = require('fs')
const path = require('path')
Expand Down
31 changes: 6 additions & 25 deletions e2e.config.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
export default {
failFast: true,
serial: true,
babel: {
testOptions: {
babelrc: false,
plugins: [
[
"@babel/plugin-transform-react-jsx",
{
pragma: "xml",
},
],
[
"babel-plugin-jsx-pragmatic",
{
module: "@xmpp/xml",
import: "xml",
},
],
],
},
},
nodeArguments: ["--experimental-require-module"],
files: ["test/*.js"],
"use strict";

/** @type {import('jest').Config} */
module.exports = {
testMatch: ["<rootDir>/test/*.js"],
setupFilesAfterEnv: ["jest-extended/all"],
};
46 changes: 30 additions & 16 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ import globals from "globals";
import eslintPluginUnicorn from "eslint-plugin-unicorn";
import eslintNodePlugin from "eslint-plugin-n";
import pluginPromise from "eslint-plugin-promise";
import pluginJest from "eslint-plugin-jest";

export default [
{
ignores: [
"**/dist/*.js",
"bundle.js",
"packages/xmpp.js/index.js",
"ava.config.js",
"e2e.config.js",
"eslint.config.mjs",
],
ignores: ["**/dist/*.js", "eslint.config.mjs"],
},
js.configs.recommended,
eslintPluginUnicorn.configs["flat/recommended"],
Expand Down Expand Up @@ -61,14 +55,9 @@ export default [
// node
// https://github.com/eslint-community/eslint-plugin-n/
"n/no-unpublished-require": 0, // doesn't play nice with monorepo
"n/no-extraneous-require": [
"error",
{ allowModules: ["ava", "sinon", "@xmpp/test"] },
],
"n/no-extraneous-import": [
"error",
{ allowModules: ["ava", "sinon", "@xmpp/test"] },
],
"n/no-extraneous-require": ["error", { allowModules: ["@xmpp/test"] }],
"n/no-extraneous-import": ["error", { allowModules: ["@xmpp/test"] }],
"n/hashbang": "off",

// promise
// https://github.com/xjamundx/eslint-plugin-promise
Expand Down Expand Up @@ -97,4 +86,29 @@ export default [
sourceType: "module",
},
},
{
files: ["**/*.spec.js", "**/*.test.js", "**/test.js", "**/test/**.js"],
plugins: { jest: pluginJest },
languageOptions: {
globals: pluginJest.environments.globals.globals,
},
rules: {
...pluginJest.configs["flat/style"].rules,
...pluginJest.configs["flat/recommended"].rules,
"jest/no-done-callback": "off",
"jest/prefer-to-be": "off",
"jest/no-conditional-expect": "off",
// https://github.com/jest-community/eslint-plugin-jest/pull/1688
"jest/valid-expect": "off",
// "jest/valid-expect": [
// "error",
// {
// alwaysAwait: true,
// // For jest-extended expect().pass
// minArgs: 0,
// },
// ],
"promise/no-callback-in-promise": "off",
},
},
];
15 changes: 15 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

// eslint-disable-next-line n/no-extraneous-require
const { defaults } = require("jest-config");

/** @type {import('jest').Config} */
module.exports = {
testMatch: [...defaults.testMatch, "**/test/*.js"],
testPathIgnorePatterns: [
...defaults.testPathIgnorePatterns,
"<rootDir>/test/",
"<rootDir>/packages/test/",
],
setupFilesAfterEnv: ["jest-extended/all"],
};
Loading

0 comments on commit 3b9114e

Please sign in to comment.