Skip to content

Commit 384d46d

Browse files
authored
Merge pull request #59 from happo/test-runner
Use node's built-in test runner
2 parents e39114e + ef16a56 commit 384d46d

9 files changed

+117
-152
lines changed

.node-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20.10.0
1+
22.11.0

run-tests.sh

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ set -euo pipefail
44
export HAPPO_ENABLED=true
55
export HAPPO_E2E_PORT=3000
66

7-
for file in ./test/*
8-
do
9-
echo ""
10-
echo "Running test $file"
11-
12-
node "$file"
13-
14-
echo "✅ Test $file passed!"
15-
done
7+
if [[ $(node -v | cut -d. -f1 | tr -d 'v') -ge 22 ]]; then
8+
node --test "./test/*"
9+
else
10+
node --test test
11+
fi

test/controller-test.js

+18-26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { it, before, after } = require('node:test');
12
const fs = require('fs');
23
const path = require('path');
34
const assert = require('assert');
@@ -14,8 +15,22 @@ const mockHappoConfig = {
1415
},
1516
};
1617

17-
async function runTest() {
18-
// Test for init
18+
const mockHappoConfigPath = path.join(__dirname, '..', '.happo.js');
19+
20+
before(() => {
21+
// Create a mock happo.js file
22+
fs.writeFileSync(
23+
mockHappoConfigPath,
24+
`module.exports = ${JSON.stringify(mockHappoConfig)}`,
25+
);
26+
});
27+
28+
after(() => {
29+
// Clean up the mock config
30+
fs.unlinkSync(mockHappoConfigPath);
31+
});
32+
33+
it('initializes with the correct happo config', async () => {
1934
const controller = new Controller();
2035
await controller.init();
2136
assert.strictEqual(controller.happoConfig.apiKey, mockHappoConfig.apiKey);
@@ -24,27 +39,4 @@ async function runTest() {
2439
assert.deepStrictEqual(controller.snapshots, []);
2540
assert.deepStrictEqual(controller.snapshotAssetUrls, []);
2641
assert.deepStrictEqual(controller.allCssBlocks, []);
27-
}
28-
29-
async function main() {
30-
const mockHappoConfigPath = path.join(__dirname, '..', '.happo.js');
31-
try {
32-
// Create a mock happo.js file
33-
fs.writeFileSync(
34-
mockHappoConfigPath,
35-
`module.exports = ${JSON.stringify(mockHappoConfig)}`,
36-
);
37-
38-
await runTest();
39-
40-
console.log('All Controller tests passed');
41-
} catch (error) {
42-
console.error('Controller tests failed:', error);
43-
process.exitCode = 1;
44-
} finally {
45-
// Clean up the mock config
46-
fs.unlinkSync(mockHappoConfigPath);
47-
}
48-
}
49-
50-
main();
42+
});

test/createAssetPackage-test.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { it } = require('node:test');
12
const assert = require('assert');
23
const fs = require('fs');
34

@@ -32,7 +33,7 @@ async function wrap(func) {
3233
}
3334
}
3435

35-
async function runTest() {
36+
it('creates an asset package', async () => {
3637
await wrap(async () => {
3738
const pkg = await createAssetPackage([
3839
{
@@ -51,11 +52,4 @@ async function runTest() {
5152
assert.equal(pkg.hash, '898862aad00d429b73f57256332a6ee1');
5253
return pkg;
5354
});
54-
}
55-
56-
runTest()
57-
.then(() => process.exit(0))
58-
.catch((e) => {
59-
console.error(e);
60-
process.exit(1);
61-
});
55+
});

test/findCSSAssetUrls-test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
const { it } = require('node:test');
12
const assert = require('assert');
23

34
const findCSSAssetUrls = require('../src/findCSSAssetUrls');
45

5-
function runTest() {
6+
it('finds asset urls in CSS', () => {
67
assert.deepEqual(
78
findCSSAssetUrls(
89
`
@@ -21,7 +22,4 @@ function runTest() {
2122
),
2223
['/bar.png', '/fonts/myfont.woff2', '/fonts/myfont.woff'],
2324
);
24-
}
25-
26-
runTest();
27-
console.log('All findCSSAssetUrls tests passed');
25+
});

test/makeAbsolute-test.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
const assert = require('assert');
1+
const { it } = require('node:test');
2+
const assert = require('node:assert');
23

34
const makeAbsolute = require('../src/makeAbsolute');
45

56
const baseUrl = 'https://base.url';
67

7-
function runTest() {
8+
it('prepends baseUrl if protocol is missing', () => {
89
assert.equal(makeAbsolute('/foo.png', baseUrl), 'https://base.url/foo.png');
9-
assert.equal(
10-
makeAbsolute('http://elsewhere.com/bar.png', baseUrl),
11-
'http://elsewhere.com/bar.png',
12-
);
13-
assert.equal(
14-
makeAbsolute('//elsewhere.com/bar.png', baseUrl),
15-
'https://elsewhere.com/bar.png',
16-
);
1710
assert.equal(
1811
makeAbsolute('/bar/foo.png', baseUrl),
1912
'https://base.url/bar/foo.png',
@@ -43,7 +36,22 @@ function runTest() {
4336
makeAbsolute('foo/bar/baz.png', 'http://goo.bar/car/'),
4437
'http://goo.bar/car/foo/bar/baz.png',
4538
);
46-
}
39+
});
40+
41+
it('returns absolute URL if protocol is present', () => {
42+
assert.equal(
43+
makeAbsolute('http://elsewhere.com/bar.png', baseUrl),
44+
'http://elsewhere.com/bar.png',
45+
);
46+
assert.equal(
47+
makeAbsolute('https://elsewhere.com/bar.png', baseUrl),
48+
'https://elsewhere.com/bar.png',
49+
);
50+
});
4751

48-
runTest();
49-
console.log('All makeAbsolute tests passed');
52+
it('handles relative protocol URLs', () => {
53+
assert.equal(
54+
makeAbsolute('//elsewhere.com/bar.png', baseUrl),
55+
'https://elsewhere.com/bar.png',
56+
);
57+
});

test/makeExternalUrlsAbsolute-test.js

+38-39
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
1+
const { it } = require('node:test');
12
const assert = require('assert');
23

34
const makeExternalUrlsAbsolute = require('../src/makeExternalUrlsAbsolute');
45

56
const resourceUrl = 'https://base.url/styles/styles.min.css';
67

7-
function runTest() {
8-
// updates urls
8+
it('updates urls', () => {
99
assert.equal(
1010
makeExternalUrlsAbsolute(
1111
`
12-
.foo {
13-
background-image: url("/bar.png");
14-
}
15-
@font-face {
16-
font-family: "MyFont";
17-
src: url('/fonts/myfont.woff2') format("woff2"),
18-
url(/fonts/myfont.woff) format("woff");
19-
}
20-
.ignore {
21-
background: url(data:image/png;base64,asdf);
22-
}
23-
.relative {
24-
background: url(../one.png);
25-
background-image: url(two.png);
26-
}
27-
`.trim(),
12+
.foo {
13+
background-image: url("/bar.png");
14+
}
15+
@font-face {
16+
font-family: "MyFont";
17+
src: url('/fonts/myfont.woff2') format("woff2"),
18+
url(/fonts/myfont.woff) format("woff");
19+
}
20+
.ignore {
21+
background: url(data:image/png;base64,asdf);
22+
}
23+
.relative {
24+
background: url(../one.png);
25+
background-image: url(two.png);
26+
}
27+
`.trim(),
2828
resourceUrl,
2929
),
3030
`
31-
.foo {
32-
background-image: url("https://base.url/bar.png");
33-
}
34-
@font-face {
35-
font-family: "MyFont";
36-
src: url('https://base.url/fonts/myfont.woff2') format("woff2"),
37-
url(https://base.url/fonts/myfont.woff) format("woff");
38-
}
39-
.ignore {
40-
background: url(data:image/png;base64,asdf);
41-
}
42-
.relative {
43-
background: url(https://base.url/one.png);
44-
background-image: url(https://base.url/styles/two.png);
45-
}
46-
`.trim(),
31+
.foo {
32+
background-image: url("https://base.url/bar.png");
33+
}
34+
@font-face {
35+
font-family: "MyFont";
36+
src: url('https://base.url/fonts/myfont.woff2') format("woff2"),
37+
url(https://base.url/fonts/myfont.woff) format("woff");
38+
}
39+
.ignore {
40+
background: url(data:image/png;base64,asdf);
41+
}
42+
.relative {
43+
background: url(https://base.url/one.png);
44+
background-image: url(https://base.url/styles/two.png);
45+
}
46+
`.trim(),
4747
);
48-
// can deal with empty input
49-
assert.equal(makeExternalUrlsAbsolute('', resourceUrl), '');
50-
}
48+
});
5149

52-
runTest();
53-
console.log('All makeExternalUrlsAbsolute tests passed');
50+
it('can deal with empty input', () => {
51+
assert.equal(makeExternalUrlsAbsolute('', resourceUrl), '');
52+
});

test/resolveEnvironment-test.js

+17-29
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
const { it } = require('node:test');
12
const path = require('path');
23
const assert = require('assert');
34
const resolveEnvironment = require('../src/resolveEnvironment');
45

5-
function testDevEnv() {
6+
it('resolves the dev environment', () => {
67
const result = resolveEnvironment({});
78
assert.equal(result.beforeSha, '__LATEST__');
89
assert.ok(/^dev-[a-z0-9]+$/.test(result.afterSha));
910
assert.equal(result.link, undefined);
1011
assert.equal(result.message, undefined);
11-
}
12+
});
1213

13-
function testCircleCIEnv() {
14+
it('resolves the CircleCI environment', () => {
1415
const circleEnv = {
1516
CI_PULL_REQUEST: 'https://ghe.com/foo/bar/pull/12',
1617
CIRCLE_PROJECT_USERNAME: 'happo',
@@ -43,9 +44,9 @@ function testCircleCIEnv() {
4344
assert.equal(result.afterSha, 'abcdef');
4445
assert.equal(result.link, 'https://github.com/happo/happo-view/commit/abcdef');
4546
assert.ok(result.message !== undefined);
46-
}
47+
});
4748

48-
function testAzureEnv() {
49+
it('resolves the Azure environment', () => {
4950
const azureEnv = {
5051
BUILD_SOURCEVERSION: '25826448f15ebcb939804ca769a00ee1df08e10d',
5152
BUILD_REPOSITORY_URI:
@@ -89,9 +90,9 @@ function testAzureEnv() {
8990
// 'https://github.com/happo/happo-view/commit/abcdef',
9091
// );
9192
// assert.ok(result.message !== undefined);
92-
}
93+
});
9394

94-
function testTagMatchingEnv() {
95+
it('resolves the tag matching environment', () => {
9596
const azureEnv = {
9697
BUILD_SOURCEVERSION: '25826448f15ebcb939804ca769a00ee1df08e10d',
9798
BUILD_REPOSITORY_URI:
@@ -110,9 +111,9 @@ function testTagMatchingEnv() {
110111

111112
assert.equal(result.afterSha, '25826448f15ebcb939804ca769a00ee1df08e10d');
112113
assert.equal(result.beforeSha, '25826448f15ebcb939804ca769a00ee1df08e10d');
113-
}
114+
});
114115

115-
function testGithubActionsEnvironment() {
116+
it('resolves the GitHub Actions environment', () => {
116117
const githubEnv = {
117118
GITHUB_SHA: 'ccddffddccffdd',
118119
GITHUB_EVENT_PATH: path.resolve(__dirname, 'github_pull_request_event.json'),
@@ -163,9 +164,9 @@ function testGithubActionsEnvironment() {
163164
caughtError.message,
164165
'Failed to load GitHub event from the GITHUB_EVENT_PATH environment variable: "non-existing-path"',
165166
);
166-
}
167+
});
167168

168-
function testGithubMergeGroupEnvironment() {
169+
it('resolves the GitHub merge group environment', () => {
169170
const githubEnv = {
170171
GITHUB_SHA: 'ccddffddccffdd',
171172
GITHUB_EVENT_PATH: path.resolve(__dirname, 'github_merge_group_event.json'),
@@ -178,9 +179,9 @@ function testGithubMergeGroupEnvironment() {
178179
'https://github.com/Codertocat/Hello-World/commit/ec26c3e57ca3a959ca5aad62de7213c562f8c821',
179180
);
180181
assert.ok(result.message !== undefined);
181-
}
182+
});
182183

183-
function testTravisEnv() {
184+
it('resolves the Travis environment', () => {
184185
const travisEnv = {
185186
HAPPO_GITHUB_BASE: 'http://git.hub',
186187
TRAVIS_REPO_SLUG: 'owner/repo',
@@ -226,9 +227,9 @@ bdac2595db20ad2a6bf335b59510aa771125526a
226227
);
227228
assert.ok(result.message !== undefined);
228229
*/
229-
}
230+
});
230231

231-
function testHappoEnv() {
232+
it('resolves the happo environment', () => {
232233
const happoEnv = {
233234
HAPPO_CURRENT_SHA: 'bdac2595db20ad2a6bf335b59510aa771125526a',
234235
HAPPO_PREVIOUS_SHA: 'hhhggg',
@@ -267,17 +268,4 @@ function testHappoEnv() {
267268
assert.equal(result.afterSha, 'bdac2595db20ad2a6bf335b59510aa771125526a');
268269
assert.equal(result.link, 'link://link');
269270
assert.ok(result.message !== undefined);
270-
}
271-
272-
function runTest() {
273-
testGithubActionsEnvironment();
274-
testGithubMergeGroupEnvironment();
275-
testDevEnv();
276-
testCircleCIEnv();
277-
testTravisEnv();
278-
testAzureEnv();
279-
testHappoEnv();
280-
testTagMatchingEnv();
281-
}
282-
runTest();
283-
console.log('All tests passed');
271+
});

0 commit comments

Comments
 (0)