Skip to content

Draft: Playground unit-test-utils module #836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/playground/unit-test-utils/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": [
"error",
{
"ignoredFiles": [
"{projectRoot}/vite.config.{js,ts,mjs,mts}"
]
}
]
}
}
]
}
11 changes: 11 additions & 0 deletions packages/playground/unit-test-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# playground-unit-test-utils

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build playground-unit-test-utils` to build the library.

## Running unit tests

Run `nx test playground-unit-test-utils` to execute the unit tests via [Jest](https://jestjs.io).
8 changes: 8 additions & 0 deletions packages/playground/unit-test-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@wp-playground/unit-test-utils",
"version": "0.0.1",
"dependencies": {},
"main": "./index.js",
"module": "./index.mjs",
"typings": "./index.d.ts"
}
34 changes: 34 additions & 0 deletions packages/playground/unit-test-utils/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "playground-unit-test-utils",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/playground/unit-test-utils/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/vite:build",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/playground/unit-test-utils"
}
},
"test": {
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"passWithNoTests": true,
"reportsDirectory": "../../../coverage/packages/playground/unit-test-utils"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"packages/playground/unit-test-utils/**/*.ts",
"packages/playground/unit-test-utils/package.json"
]
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions packages/playground/unit-test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/playground-unit-test-utils';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { playgroundUnitTestUtils } from './playground-unit-test-utils';

describe('playgroundUnitTestUtils', () => {
it('should work', () => {
expect(playgroundUnitTestUtils()).toEqual('playground-unit-test-utils');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { readFileSync } from 'fs';

export async function getWordPressDataModule() {
const wpData = readFileSync(
__dirname + '/../../../remote/src/wordpress/wp-6.3.data'
);
const wpDataArrayBuffer = wpData.buffer.slice(
wpData.byteOffset,
wpData.byteOffset + wpData.byteLength
);
shimXHR(wpDataArrayBuffer);
// @ts-ignore
return await import(__dirname + '/../../../remote/src/wordpress/wp-6.3.js');
}

function shimXHR(response: ArrayBuffer) {
// Shim XMLHttpRequest to return a fixed response
// @ts-ignore
globalThis.XMLHttpRequest = class XMLHttpRequest {
response?: ArrayBuffer;
onload() {}
open() {
setTimeout(() => {
this.response = response;
this.onload();
}, 100);
}
send() {}
setRequestHeader() {}
getResponseHeader() {}
getAllResponseHeaders() {}
abort() {}
addEventListener() {}
removeEventListener() {}
dispatchEvent() {}
get readyState() {
return 4;
}
get status() {
return 200;
}
};
}
22 changes: 22 additions & 0 deletions packages/playground/unit-test-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
10 changes: 10 additions & 0 deletions packages/playground/unit-test-utils/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"declaration": true,
"types": ["node", "vite/client"]
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
25 changes: 25 additions & 0 deletions packages/playground/unit-test-utils/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": [
"vitest/globals",
"vitest/importMeta",
"vite/client",
"node",
"vitest"
]
},
"include": [
"vite.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx",
"src/**/*.test.js",
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts"
]
}
50 changes: 50 additions & 0 deletions packages/playground/unit-test-utils/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// <reference types='vitest' />
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import * as path from 'path';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';

export default defineConfig({
cacheDir: '../../../node_modules/.vite/playground-unit-test-utils',

plugins: [
nxViteTsPaths(),
dts({
entryRoot: 'src',
tsConfigFilePath: path.join(__dirname, 'tsconfig.lib.json'),
skipDiagnostics: true,
}),
],

// Uncomment this if you are using workers.
// worker: {
// plugins: [ nxViteTsPaths() ],
// },

// Configuration for building your library.
// See: https://vitejs.dev/guide/build.html#library-mode
build: {
lib: {
// Could also be a dictionary or array of multiple entry points.
entry: 'src/index.ts',
name: 'playground-unit-test-utils',
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es', 'cjs'],
},
rollupOptions: {
// External packages that should not be bundled into your library.
external: [],
},
},

test: {
globals: true,
cache: {
dir: '../../../node_modules/.vitest',
},
environment: 'node',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
});
3 changes: 3 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"packages/playground/storage/src/index.ts"
],
"@wp-playground/sync": ["packages/playground/sync/src/index.ts"],
"@wp-playground/unit-test-utils": [
"packages/playground/unit-test-utils/src/index.ts"
],
"@wp-playground/website": [
"packages/playground/website/src/index.ts"
]
Expand Down