Skip to content

Commit 364cf64

Browse files
committed
Localize test setup and use ESM for tests
I wanted to run the controller test on its own, but it failed because these env vars were set in the shell script. I started by adding a setup.js file and importing it in all of the tests but then I realized that we only need to do this in the controller test so I moved it there. In the middle I ended up converting these files to ESM.
1 parent ac0731c commit 364cf64

8 files changed

+56
-35
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
ignorePatterns: ['pages/'],
2020
parserOptions: {
2121
sourceType: 'module',
22-
ecmaVersion: 2018,
22+
ecmaVersion: 2020,
2323
},
2424
rules: {
2525
'no-use-before-define': 2,

test/controller-test.js test/controller-test.mjs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const { describe, it, before, after } = require('node:test');
2-
const fs = require('fs');
3-
const path = require('path');
4-
const assert = require('assert');
5-
const Controller = require('../controller');
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import assert from 'node:assert';
4+
import { describe, it, before, after } from 'node:test';
5+
6+
import Controller from '../controller.js';
67

78
const mockHappoConfig = {
89
apiKey: 'test-api-key',
@@ -15,9 +16,17 @@ const mockHappoConfig = {
1516
},
1617
};
1718

18-
const mockHappoConfigPath = path.join(__dirname, '..', '.happo.js');
19+
const mockHappoConfigPath = path.join(import.meta.dirname, '..', '.happo.js');
20+
21+
let originalEnv = {
22+
HAPPO_ENABLED: process.env.HAPPO_ENABLED,
23+
HAPPO_E2E_PORT: process.env.HAPPO_E2E_PORT,
24+
};
1925

2026
before(() => {
27+
process.env.HAPPO_ENABLED = true;
28+
process.env.HAPPO_E2E_PORT = 3000;
29+
2130
// Create a mock happo.js file
2231
fs.writeFileSync(
2332
mockHappoConfigPath,
@@ -26,6 +35,10 @@ before(() => {
2635
});
2736

2837
after(() => {
38+
for (const [key, value] of Object.entries(originalEnv)) {
39+
process.env[key] = value;
40+
}
41+
2942
// Clean up the mock config
3043
fs.unlinkSync(mockHappoConfigPath);
3144
});

test/createAssetPackage-test.js test/createAssetPackage-test.mjs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
const { describe, it } = require('node:test');
2-
const assert = require('assert');
3-
const fs = require('fs');
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert';
3+
import fs from 'node:fs';
4+
import http from 'node:http';
5+
import handler from 'serve-handler';
46

5-
const createAssetPackage = require('../src/createAssetPackage');
7+
import createAssetPackage from '../src/createAssetPackage.js';
68

79
const { SAVE_PACKAGE } = process.env;
810

911
async function wrap(func) {
10-
const handler = require('serve-handler');
11-
const http = require('http');
12-
1312
const server = http.createServer((request, response) => {
1413
return handler(request, response, { public: 'test-fixtures' });
1514
});

test/findCSSAssetUrls-test.js test/findCSSAssetUrls-test.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { describe, it } = require('node:test');
2-
const assert = require('assert');
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert';
33

4-
const findCSSAssetUrls = require('../src/findCSSAssetUrls');
4+
import findCSSAssetUrls from '../src/findCSSAssetUrls.js';
55

66
describe('findCSSAssetUrls', () => {
77
it('finds asset urls in CSS', () => {

test/makeAbsolute-test.js test/makeAbsolute-test.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { describe, it } = require('node:test');
2-
const assert = require('node:assert');
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert';
33

4-
const makeAbsolute = require('../src/makeAbsolute');
4+
import makeAbsolute from '../src/makeAbsolute.js';
55

66
const baseUrl = 'https://base.url';
77

test/makeExternalUrlsAbsolute-test.js test/makeExternalUrlsAbsolute-test.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { describe, it } = require('node:test');
2-
const assert = require('assert');
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert';
33

4-
const makeExternalUrlsAbsolute = require('../src/makeExternalUrlsAbsolute');
4+
import makeExternalUrlsAbsolute from '../src/makeExternalUrlsAbsolute.js';
55

66
const resourceUrl = 'https://base.url/styles/styles.min.css';
77

test/resolveEnvironment-test.js test/resolveEnvironment-test.mjs

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { describe, it } = require('node:test');
2-
const path = require('path');
3-
const assert = require('assert');
4-
const resolveEnvironment = require('../src/resolveEnvironment');
1+
import { describe, it } from 'node:test';
2+
import path from 'node:path';
3+
import assert from 'node:assert';
4+
import resolveEnvironment from '../src/resolveEnvironment.js';
55

66
describe('resolveEnvironment', () => {
77
it('resolves the dev environment', () => {
@@ -117,7 +117,10 @@ describe('resolveEnvironment', () => {
117117
it('resolves the GitHub Actions environment', () => {
118118
const githubEnv = {
119119
GITHUB_SHA: 'ccddffddccffdd',
120-
GITHUB_EVENT_PATH: path.resolve(__dirname, 'github_pull_request_event.json'),
120+
GITHUB_EVENT_PATH: path.resolve(
121+
import.meta.dirname,
122+
'github_pull_request_event.json',
123+
),
121124
};
122125
let result = resolveEnvironment(githubEnv);
123126
assert.equal(result.beforeSha, 'f95f852bd8fca8fcc58a9a2d6c842781e32a215e');
@@ -126,7 +129,10 @@ describe('resolveEnvironment', () => {
126129
assert.equal(result.message, 'Update the README with new information.');
127130

128131
// Try with a push event
129-
githubEnv.GITHUB_EVENT_PATH = path.resolve(__dirname, 'github_push_event.json');
132+
githubEnv.GITHUB_EVENT_PATH = path.resolve(
133+
import.meta.dirname,
134+
'github_push_event.json',
135+
);
130136
result = resolveEnvironment(githubEnv);
131137
assert.equal(result.beforeSha, '6113728f27ae82c7b1a177c8d03f9e96e0adf246');
132138
assert.equal(result.afterSha, '0000000000000000000000000000000000000000');
@@ -138,7 +144,7 @@ describe('resolveEnvironment', () => {
138144

139145
// Try with a workflow_dispatch event
140146
githubEnv.GITHUB_EVENT_PATH = path.resolve(
141-
__dirname,
147+
import.meta.dirname,
142148
'github_workflow_dispatch.json',
143149
);
144150
result = resolveEnvironment(githubEnv);
@@ -170,7 +176,10 @@ describe('resolveEnvironment', () => {
170176
it('resolves the GitHub merge group environment', () => {
171177
const githubEnv = {
172178
GITHUB_SHA: 'ccddffddccffdd',
173-
GITHUB_EVENT_PATH: path.resolve(__dirname, 'github_merge_group_event.json'),
179+
GITHUB_EVENT_PATH: path.resolve(
180+
import.meta.dirname,
181+
'github_merge_group_event.json',
182+
),
174183
};
175184
let result = resolveEnvironment(githubEnv);
176185
assert.equal(result.beforeSha, 'f95f852bd8fca8fcc58a9a2d6c842781e32a215e');

test/takeDOMSnapshot-test.js test/takeDOMSnapshot-test.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { describe, it } = require('node:test');
2-
const assert = require('assert');
3-
const jsdom = require('jsdom');
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert';
3+
import jsdom from 'jsdom';
44

5-
const takeDOMSnapshot = require('../takeDOMSnapshot');
5+
import takeDOMSnapshot from '../takeDOMSnapshot.js';
66

77
describe('takeDOMSnapshot', () => {
88
it('takes a basic snapshot', () => {

0 commit comments

Comments
 (0)