Skip to content

Commit 100407b

Browse files
Merge pull request #17 from matlab-actions/update-tests-to-use-esm
Update tests to use ESM
2 parents f4623e1 + 3cbcd48 commit 100407b

8 files changed

Lines changed: 87 additions & 66 deletions

jest.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
export default {
2+
clearMocks: true,
23
testEnvironment: "node",
34
collectCoverage: true,
45
testMatch: ["**/*.test.ts"],
56
transform: {
67
"^.+\\.[jt]s$": [
78
"ts-jest",
89
{
10+
useESM: true,
911
diagnostics: {
1012
ignoreCodes: [151002],
1113
},
1214
},
1315
],
1416
},
17+
extensionsToTreatAsEsm: ['.ts'],
1518
transformIgnorePatterns: ["node_modules/(?!(@actions)/)"],
1619
moduleNameMapper: {
1720
"^(\\.{1,2}/.*)\\.js$": "$1",
18-
"^@actions/core$": "<rootDir>/node_modules/@actions/core/lib/core.js",
19-
"^@actions/exec$": "<rootDir>/node_modules/@actions/exec/lib/exec.js",
2021
},
2122
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"build": "tsc",
1313
"prepare": "npm run build",
1414
"package": "ncc build --minify",
15-
"test": "jest",
15+
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
1616
"all": "npm test && npm run build && npm run package",
1717
"ci": "npm run clean && npm ci --ignore-scripts && npm run all"
1818
},

src/buildSummary.unit.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
// Copyright 2024-26 The MathWorks, Inc.
22

3-
import * as buildSummary from "./buildSummary.js";
4-
import * as core from "@actions/core";
3+
import { jest, describe, it, expect, beforeEach } from "@jest/globals";
54

6-
jest.mock('@actions/core', () => ({
5+
jest.unstable_mockModule('@actions/core', () => ({
76
summary: {
87
addTable: jest.fn().mockReturnThis(),
98
addHeading: jest.fn().mockReturnThis(),
109
write: jest.fn().mockReturnThis(),
1110
},
1211
}));
1312

13+
const core = await import("@actions/core");
14+
const buildSummary = await import("./buildSummary.js");
15+
16+
beforeEach(() => {
17+
(core.summary.addTable as jest.Mock).mockReturnThis();
18+
(core.summary.addHeading as jest.Mock).mockReturnThis();
19+
(core.summary.write as jest.Mock).mockReturnThis();
20+
});
21+
1422
describe('summaryGeneration', () => {
1523
it('should process and return summary rows for valid JSON with different task statuses', () => {
1624
const mockBuildSummary = JSON.stringify([

src/matlab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export function getRunMATLABCommandScriptPath(platform: string, architecture: st
103103
default:
104104
throw new Error(`This action is not supported on ${platform} runners using the ${architecture} architecture.`);
105105
}
106-
const rmcPath = path.join(__dirname, "bin", platformDir, `run-matlab-command${ext}`);
106+
const rmcPath = path.join(import.meta.dirname, "bin", platformDir, `run-matlab-command${ext}`);
107107
return rmcPath;
108108

109109
}

src/matlab.unit.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright 2020-2026 The MathWorks, Inc.
22

3+
import { jest, describe, it, expect, afterEach } from "@jest/globals";
34
import { promises as fs } from "fs";
45
import * as path from "path";
56
import * as matlab from "./matlab.js";
7+
import type { ExecFn } from "./matlab.js";
68

79
afterEach(() => {
810
jest.resetAllMocks();
@@ -62,7 +64,7 @@ describe("run command", () => {
6264

6365
it("ideally works", async () => {
6466
const chmod = jest.spyOn(fs, "chmod");
65-
const execFn = jest.fn();
67+
const execFn = jest.fn<ExecFn>();
6668

6769
chmod.mockResolvedValue(undefined);
6870
execFn.mockResolvedValue(0);
@@ -73,21 +75,21 @@ describe("run command", () => {
7375

7476
it("ideally works with arguments", async () => {
7577
const chmod = jest.spyOn(fs, "chmod");
76-
const execFn = jest.fn();
78+
const execFn = jest.fn<ExecFn>();
7779

7880
chmod.mockResolvedValue(undefined);
7981
execFn.mockResolvedValue(0);
8082

8183
const actual = matlab.runCommand(helperScript, platform, architecture, execFn, ["-nojvm", "-logfile", "file"]);
8284
await expect(actual).resolves.toBeUndefined();
83-
expect(execFn.mock.calls[0][1][1]).toBe("-nojvm");
84-
expect(execFn.mock.calls[0][1][2]).toBe("-logfile");
85-
expect(execFn.mock.calls[0][1][3]).toBe("file");
85+
expect(execFn.mock.calls[0][1]![1]).toBe("-nojvm");
86+
expect(execFn.mock.calls[0][1]![2]).toBe("-logfile");
87+
expect(execFn.mock.calls[0][1]![3]).toBe("file");
8688
});
8789

8890
it("fails when chmod fails", async () => {
8991
const chmod = jest.spyOn(fs, "chmod");
90-
const execFn = jest.fn();
92+
const execFn = jest.fn<ExecFn>();
9193

9294
chmod.mockRejectedValue(null);
9395

@@ -99,7 +101,7 @@ describe("run command", () => {
99101

100102
it("fails when the execFn fails", async () => {
101103
const chmod = jest.spyOn(fs, "chmod");
102-
const execFn = jest.fn();
104+
const execFn = jest.fn<ExecFn>();
103105

104106
chmod.mockResolvedValue(undefined);
105107
execFn.mockRejectedValue(null);
@@ -112,7 +114,7 @@ describe("run command", () => {
112114

113115
it("fails when the execFn has a non-zero exit code", async () => {
114116
const chmod = jest.spyOn(fs, "chmod");
115-
const execFn = jest.fn();
117+
const execFn = jest.fn<ExecFn>();
116118

117119
chmod.mockResolvedValue(undefined);
118120
execFn.mockResolvedValue(1);

src/script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as path from "path";
1010
* @returns MATLAB command.
1111
*/
1212
export function prepare(command: string): string {
13-
const pluginsPath = path.join(__dirname, "plugins").replaceAll("'","''");
13+
const pluginsPath = path.join(import.meta.dirname, "plugins").replaceAll("'","''");
1414
return `cd(getenv('MW_ORIG_WORKING_FOLDER')); ` +
1515
`addpath('` + pluginsPath + `'); `
1616
+ command;

src/script.unit.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright 2020-2026 The MathWorks, Inc.
22

3+
import { describe, it, expect } from "@jest/globals";
34
import * as script from "./script.js";
45
import * as path from "path";
56

67
describe("call generation", () => {
78
it("ideally works", () => {
89
// I know what your thinking
910
const testCommand = "disp('hello world')";
10-
const pluginsPath = path.join(__dirname, "plugins").replaceAll("'","''");
11+
const pluginsPath = path.join(import.meta.dirname, "plugins").replaceAll("'","''");
1112
const expectedString = `cd(getenv('MW_ORIG_WORKING_FOLDER')); addpath('` + pluginsPath + `'); ${testCommand}`;
1213

1314
expect(script.prepare(testCommand)).toMatch(expectedString);

0 commit comments

Comments
 (0)