mirror of
https://github.com/flarum/core.git
synced 2025-08-05 16:07:34 +02:00
feat(jest): create jest config package for unit testing (#3678)
* feat(jest): create jest config package for unit testing * chore: housekeeping * fix: now we need to explicitly allow importing without extension * fix: recover EditorDriverInterface import * Apply suggestions from code review * chore: yarn Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
This commit is contained in:
13
js-packages/jest-config/.gitignore
vendored
Normal file
13
js-packages/jest-config/.gitignore
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
node_modules
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
.vscode
|
||||
|
||||
# https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
|
||||
.pnp.*
|
||||
.yarn/*
|
||||
!.yarn/patches
|
||||
!.yarn/plugins
|
||||
!.yarn/releases
|
||||
!.yarn/sdks
|
||||
!.yarn/versions
|
22
js-packages/jest-config/LICENSE
Executable file
22
js-packages/jest-config/LICENSE
Executable file
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2022 Stichting Flarum (Flarum Foundation)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
14
js-packages/jest-config/README.md
Executable file
14
js-packages/jest-config/README.md
Executable file
@@ -0,0 +1,14 @@
|
||||
# Jest config for Flarum
|
||||
|
||||
This package provides a [Jest](https://jestjs.io/) config object to run unit & integration tests on Flarum extensions.
|
||||
|
||||
## Usage
|
||||
|
||||
* Install the package: `yarn add --dev flarum-jest-config`
|
||||
* Add `"type": "module"` to your `package.json`
|
||||
* Add `"test": "yarn node --experimental-vm-modules $(yarn bin jest)"` to your `package.json` scripts
|
||||
* Rename `webpack.config.js` to `webpack.config.cjs`
|
||||
* Create a `jest.config.cjs` file with the following content:
|
||||
```js
|
||||
module.exports = require('flarum-jest-config')();
|
||||
```
|
22
js-packages/jest-config/index.cjs
Normal file
22
js-packages/jest-config/index.cjs
Normal file
@@ -0,0 +1,22 @@
|
||||
const path = require('path');
|
||||
|
||||
module.exports = (options = {}) => ({
|
||||
testEnvironment: 'jsdom',
|
||||
extensionsToTreatAsEsm: ['.ts', '.tsx'],
|
||||
transform: {
|
||||
'^.+\\.[tj]sx?$': [
|
||||
'babel-jest',
|
||||
require('flarum-webpack-config/babel.config.js'),
|
||||
],
|
||||
'^.+\\.tsx?$': [
|
||||
'ts-jest',
|
||||
{
|
||||
useESM: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
preset: 'ts-jest',
|
||||
setupFilesAfterEnv: [path.resolve(__dirname, 'setup-env.js')],
|
||||
moduleDirectories: ['node_modules', 'src'],
|
||||
...options,
|
||||
});
|
21
js-packages/jest-config/package.json
Normal file
21
js-packages/jest-config/package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "flarum-jest-config",
|
||||
"version": "1.0.0",
|
||||
"description": "Jest config for Flarum.",
|
||||
"main": "index.cjs",
|
||||
"author": "Flarum Team",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"prettier": "@flarum/prettier-config",
|
||||
"dependencies": {
|
||||
"@types/jest": "^29.2.2",
|
||||
"flat": "^5.0.2",
|
||||
"jest": "^29.3.1",
|
||||
"jest-environment-jsdom": "^29.3.1",
|
||||
"js-yaml": "^4.1.0",
|
||||
"ts-jest": "^29.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "^2.4.1"
|
||||
}
|
||||
}
|
39
js-packages/jest-config/setup-env.js
Normal file
39
js-packages/jest-config/setup-env.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import app from '@flarum/core/src/forum/app';
|
||||
import ForumApplication from '@flarum/core/src/forum/ForumApplication';
|
||||
import jsYaml from 'js-yaml';
|
||||
import fs from 'fs';
|
||||
import jquery from 'jquery';
|
||||
import m from 'mithril';
|
||||
import flatten from 'flat';
|
||||
|
||||
// Boot the Flarum app.
|
||||
function bootApp() {
|
||||
ForumApplication.prototype.mount = () => {};
|
||||
window.flarum = { extensions: {} };
|
||||
app.load({
|
||||
apiDocument: null,
|
||||
locale: 'en',
|
||||
locales: {},
|
||||
resources: [
|
||||
{
|
||||
type: 'forums',
|
||||
id: '1',
|
||||
attributes: {},
|
||||
},
|
||||
],
|
||||
session: {
|
||||
userId: 0,
|
||||
csrfToken: 'test',
|
||||
},
|
||||
});
|
||||
app.translator.addTranslations(flatten(jsYaml.load(fs.readFileSync('../locale/core.yml', 'utf8'))));
|
||||
app.bootExtensions(window.flarum.extensions);
|
||||
app.boot();
|
||||
}
|
||||
|
||||
beforeAll(() => {
|
||||
window.$ = jquery;
|
||||
window.m = m;
|
||||
|
||||
bootApp();
|
||||
});
|
Reference in New Issue
Block a user