Skip to content

Commit

Permalink
feat(web-test-runner): add initial plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
robby rabbitman committed Jun 29, 2024
1 parent 6c9f512 commit cfb0219
Show file tree
Hide file tree
Showing 22 changed files with 2,296 additions and 234 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
47 changes: 47 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"root": true,
"ignorePatterns": ["**/*"],
"plugins": ["@nx"],
"overrides": [
{
"files": "*.json",
"parser": "jsonc-eslint-parser",
"rules": {}
},
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
"@nx/enforce-module-boundaries": [
"error",
{
"enforceBuildableLibDependency": true,
"allow": [],
"depConstraints": [
{
"sourceTag": "*",
"onlyDependOnLibsWithTags": ["*"]
}
]
}
]
}
},
{
"files": ["*.ts", "*.tsx"],
"extends": ["plugin:@nx/typescript"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"extends": ["plugin:@nx/javascript"],
"rules": {}
},
{
"files": ["*.spec.ts", "*.spec.tsx", "*.spec.js", "*.spec.jsx"],
"env": {
"jest": true
},
"rules": {}
}
]
}
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Add files here to ignore them from prettier formatting
/dist
/coverage
/.nx/cache
/.nx/workspace-data
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
6 changes: 5 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"recommendations": ["nrwl.angular-console", "esbenp.prettier-vscode"]
"recommendations": [
"nrwl.angular-console",
"esbenp.prettier-vscode",
"firsttris.vscode-jest-runner"
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint.validate": ["json"]
}
5 changes: 5 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getJestProjectsAsync } from '@nx/jest';

export default async () => ({
projects: await getJestProjectsAsync(),
});
3 changes: 3 additions & 0 deletions jest.preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const nxPreset = require('@nx/jest/preset').default;

module.exports = { ...nxPreset };
24 changes: 24 additions & 0 deletions libs/web-test-runner/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
},
{
"files": ["./package.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/nx-plugin-checks": "error"
}
}
]
}
1 change: 1 addition & 0 deletions libs/web-test-runner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# web-test-runner
10 changes: 10 additions & 0 deletions libs/web-test-runner/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'web-test-runner',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/web-test-runner',
};
15 changes: 15 additions & 0 deletions libs/web-test-runner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@robby-rabbitman/web-test-runner",
"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0",
"@nx/devkit": "19.3.2"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"exports": {
".": "./src/index.js",
"./plugin": "./src/plugin.js"
}
}
39 changes: 39 additions & 0 deletions libs/web-test-runner/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "web-test-runner",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"tags": ["scope:node", "type:plugin"],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/{projectRoot}",
"main": "{projectRoot}/src/index.ts",
"tsConfig": "{projectRoot}/tsconfig.lib.json",
"assets": [
"{projectRoot}/*.md",
{
"input": "{projectRoot}/src",
"glob": "**/!(*.ts)",
"output": "./src"
},
{
"input": "{projectRoot}/src",
"glob": "**/*.d.ts",
"output": "./src"
},
{
"input": "{projectRoot}",
"glob": "generators.json",
"output": "."
},
{
"input": "{projectRoot}",
"glob": "executors.json",
"output": "."
}
]
}
}
}
}
1 change: 1 addition & 0 deletions libs/web-test-runner/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
78 changes: 78 additions & 0 deletions libs/web-test-runner/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
CreateNodesFunction,
CreateNodesV2,
createNodesFromFiles,
getPackageManagerCommand,
} from '@nx/devkit';
import { existsSync } from 'node:fs';
import { dirname, join } from 'node:path';

type WebTestRunnerTargetPluginOptions = { targetName: string };

const webTestRunnerConfigFileNameGlob = '**/wtr.config.@(js|cjs|mjs)';

const packageManagerCommand = getPackageManagerCommand();

const defaultTargetName = 'test';

/**
* In `targets` in a `project.json`, nx replaces this symbol with the relative path to the project from the workspace root.
*/
const projectRootSymbol = '{projectRoot}';

export const createNodesV2: CreateNodesV2<WebTestRunnerTargetPluginOptions> = [
webTestRunnerConfigFileNameGlob,
(webTestRunnerConfigFileNames, options, context) => {
return createNodesFromFiles(
createWebTestRunnerTarget,
webTestRunnerConfigFileNames,
options,
context
);
},
];

const createWebTestRunnerTarget: CreateNodesFunction = (
webTestRunnerConfigFileName: string,
options: WebTestRunnerTargetPluginOptions
) => {
const webTestRunnerConfigDirectory = dirname(webTestRunnerConfigFileName);

const isWebTestRunnerConfigInProject = isProject(
webTestRunnerConfigDirectory
);

// make sure the config is in a project: a user may have a shared config located e.g. in the root of the workspace which matches the `wtrConfigFileNameRegEx`
if (!isWebTestRunnerConfigInProject) {
return {};
}

const targetName = options?.targetName ?? defaultTargetName;

return {
projects: {
[webTestRunnerConfigDirectory]: {
targets: {
[targetName]: {
command: `${packageManagerCommand.exec} wtr --config=${webTestRunnerConfigFileNameGlob}`,
options: {
cwd: `${projectRootSymbol}`,
},
},
},
},
},
};
};

/**
*
* @param directory
* @returns whether the directory is considered a nx project.
*/
const isProject = (directory: string) => {
return (
existsSync(join(directory, 'project.json')) ||
existsSync(join(directory, 'package.json'))
);
};
17 changes: 17 additions & 0 deletions libs/web-test-runner/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"types": ["node"]
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
8 changes: 8 additions & 0 deletions libs/web-test-runner/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts"]
}
12 changes: 12 additions & 0 deletions libs/web-test-runner/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
44 changes: 43 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,47 @@
"default": ["{projectRoot}/**/*", "sharedGlobals"],
"sharedGlobals": []
},
"nxCloudAccessToken": "NGU2YzEyMDItYTU4OC00YzViLTllNWEtNGY5ZjE3Y2U1Yjk0fHJlYWQtd3JpdGU="
"plugins": [
{
"plugin": "@nx/eslint/plugin",
"options": {
"targetName": "lint"
}
},
{
"plugin": "@nx/jest/plugin",
"options": {
"targetName": "test"
}
}
],
"targetDefaults": {
"@nx/js:tsc": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["default", "^default"]
},
"@nx/eslint:lint": {
"cache": true,
"inputs": [
"default",
"{workspaceRoot}/.eslintrc.json",
"{workspaceRoot}/.eslintignore",
"{workspaceRoot}/eslint.config.js"
]
},
"@nx/jest:jest": {
"cache": true,
"inputs": ["default", "^default", "{workspaceRoot}/jest.preset.js"],
"options": {
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
}
}
26 changes: 25 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,33 @@
"packageManager": "[email protected]",
"scripts": {},
"devDependencies": {
"@nx/eslint": "19.3.2",
"@nx/eslint-plugin": "19.3.2",
"@nx/jest": "19.3.2",
"@nx/js": "19.3.2",
"@nx/plugin": "19.3.2",
"@nx/workspace": "19.3.2",
"nx": "19.3.2"
"@swc-node/register": "~1.9.1",
"@swc/cli": "~0.3.12",
"@swc/core": "~1.5.7",
"@swc/helpers": "~0.5.11",
"@types/jest": "^29.4.0",
"@types/node": "18.16.9",
"@typescript-eslint/eslint-plugin": "^7.3.0",
"@typescript-eslint/parser": "^7.3.0",
"eslint": "~8.57.0",
"eslint-config-prettier": "^9.0.0",
"jest": "^29.4.1",
"jest-environment-jsdom": "^29.4.1",
"jest-environment-node": "^29.4.1",
"nx": "19.3.2",
"prettier": "^2.6.2",
"ts-jest": "^29.1.0",
"ts-node": "10.9.1",
"typescript": "~5.4.2"
},
"dependencies": {
"@nx/devkit": "19.3.2",
"tslib": "^2.3.0"
}
}
Loading

0 comments on commit cfb0219

Please sign in to comment.