Skip to content

Commit 951c87d

Browse files
author
David Buzinski
committed
Got tests to fail with __dirname. Updated tests to use ESM. Updated test code to be ESM compatible
1 parent f4623e1 commit 951c87d

8 files changed

Lines changed: 96 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { promises as fs } from "fs";
44
import * as os from "os";
55
import * as path from "path";
6+
import { fileURLToPath } from "url";
67
import { v4 as uuid } from "uuid";
78
import * as script from "./script.js";
89

@@ -103,7 +104,8 @@ export function getRunMATLABCommandScriptPath(platform: string, architecture: st
103104
default:
104105
throw new Error(`This action is not supported on ${platform} runners using the ${architecture} architecture.`);
105106
}
106-
const rmcPath = path.join(__dirname, "bin", platformDir, `run-matlab-command${ext}`);
107+
const dirname = path.dirname(fileURLToPath(import.meta.url));
108+
const rmcPath = path.join(dirname, "bin", platformDir, `run-matlab-command${ext}`);
107109
return rmcPath;
108110

109111
}

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2020-2025 The MathWorks, Inc.
22

33
import * as path from "path";
4+
import { fileURLToPath } from "url";
45

56
/**
67
* Generate MATLAB command for changing directories, adding plugins to path and calling a command in it.
@@ -10,7 +11,8 @@ import * as path from "path";
1011
* @returns MATLAB command.
1112
*/
1213
export function prepare(command: string): string {
13-
const pluginsPath = path.join(__dirname, "plugins").replaceAll("'","''");
14+
const dirname = path.dirname(fileURLToPath(import.meta.url));
15+
const pluginsPath = path.join(dirname, "plugins").replaceAll("'","''");
1416
return `cd(getenv('MW_ORIG_WORKING_FOLDER')); ` +
1517
`addpath('` + pluginsPath + `'); `
1618
+ command;

src/script.unit.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
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";
6+
import { fileURLToPath } from "url";
57

68
describe("call generation", () => {
79
it("ideally works", () => {
810
// I know what your thinking
911
const testCommand = "disp('hello world')";
10-
const pluginsPath = path.join(__dirname, "plugins").replaceAll("'","''");
12+
const dirname = path.dirname(fileURLToPath(import.meta.url));
13+
const pluginsPath = path.join(dirname, "plugins").replaceAll("'","''");
1114
const expectedString = `cd(getenv('MW_ORIG_WORKING_FOLDER')); addpath('` + pluginsPath + `'); ${testCommand}`;
1215

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

0 commit comments

Comments
 (0)