Skip to content

Commit

Permalink
test(create-plugin): adding teardown process for e2e test (#2735)
Browse files Browse the repository at this point in the history
Co-authored-by: Tuan Pham <tuan-pham@cybozu.vn>
  • Loading branch information
hung-cybo and tuanphamcybozu authored May 3, 2024
1 parent 57af1c7 commit 48f51dd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 63 deletions.
21 changes: 21 additions & 0 deletions packages/create-plugin/JestCustomEnvironment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { TestEnvironment } = require("jest-environment-node");

class JestCustomEnvironment extends TestEnvironment {
handleTestEvent(event) {
if (event.name === "test_start") {
const testName = event.test.name;
console.log(`Running test: ${testName}`);
}

if (!this.global.testStatuses) {
this.global.testStatuses = {};
}

if (event.name === "test_fn_success") {
const testName = event.test.name;
this.global.testStatuses[testName] = "passed";
}
}
}

module.exports = JestCustomEnvironment;
17 changes: 15 additions & 2 deletions packages/create-plugin/__e2e__/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,20 @@ describe("create-plugin", function () {
});

afterEach(() => {
rimrafSync(workingDir);
console.log(`Working directory ${workingDir} has been removed`);
const testName = expect.getState().currentTestName;
if (!testName || !workingDir) {
return;
}

// @ts-ignore
const match = Object.keys(globalThis.testStatuses).find((item: string) =>
testName.includes(item),
);

// @ts-ignore
if (match && globalThis.testStatuses[match] === "passed") {
rimrafSync(workingDir);
console.log(`Working directory ${workingDir} has been removed`);
}
});
});
2 changes: 1 addition & 1 deletion packages/create-plugin/jest.e2e.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const config = {
roots: ["<rootDir>"],
testRegex: "/__e2e__/.*\\.test\\.ts$",
testEnvironment: "node",
testEnvironment: "./JestCustomEnvironment.js",
testTimeout: 120000,
};
module.exports = config;
3 changes: 2 additions & 1 deletion packages/create-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"@types/lodash": "^4.17.0",
"@types/node-rsa": "^1.1.4",
"@types/normalize-path": "^3.0.2",
"cross-env": "^7.0.3"
"cross-env": "^7.0.3",
"jest-environment-node": "^29.7.0"
},
"files": [
"bin",
Expand Down
7 changes: 7 additions & 0 deletions packages/create-plugin/types/global/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@
declare interface Element {}
declare interface Document {}
declare interface NodeListOf<T = {}> {}

/* eslint-disable no-var,vars-on-top */
// We cannot use let,const to declare global object:(
// https://stackoverflow.com/a/69230938
declare global {
var testStatuses: { [testPath: string]: "failed" | "passed" | "running" };
}
93 changes: 34 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 48f51dd

Please sign in to comment.