Skip to content

Commit f30a9d0

Browse files
authored
test: remove zx globals from unit tests (#1039)
closes #1036
1 parent 131e6c9 commit f30a9d0

7 files changed

+80
-73
lines changed

test/all.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import './cli.test.js'
1616
import './core.test.js'
1717
import './deps.test.js'
1818
import './error.test.ts'
19+
import './export.test.js'
1920
import './global.test.js'
2021
import './goods.test.js'
2122
import './index.test.js'
2223
import './package.test.js'
2324
import './util.test.js'
24-
import './yaml.test.js'
25-
import './export.test.js'
25+
import './vendor.test.js'

test/cli.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { test, describe, before, after } from 'node:test'
1717
import { fileURLToPath } from 'node:url'
1818
import net from 'node:net'
1919
import getPort from 'get-port'
20-
import '../build/globals.js'
2120
import {
2221
argv,
2322
importPath,
@@ -32,6 +31,7 @@ import {
3231
transformMarkdown,
3332
writeAndImport,
3433
} from '../build/cli.js'
34+
import { $, path, fs, tmpfile, tmpdir } from '../build/index.js'
3535

3636
const __filename = fileURLToPath(import.meta.url)
3737
const spawn = $.spawn

test/global.test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
// limitations under the License.
1414

1515
import assert from 'node:assert'
16-
import { test, describe } from 'node:test'
16+
import { test, describe, after } from 'node:test'
1717
import '../build/globals.js'
1818
import * as index from '../build/index.js'
1919

2020
describe('global', () => {
21+
after(() => {
22+
for (const key of Object.keys(index)) {
23+
delete global[key]
24+
}
25+
})
26+
2127
test('global cd()', async () => {
2228
const cwd = (await $`pwd`).toString().trim()
2329
cd('/')

test/goods.test.js

+2-41
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import chalk from 'chalk'
1615
import assert from 'node:assert'
1716
import { test, describe } from 'node:test'
18-
import '../build/globals.js'
17+
import { $, chalk } from '../build/index.js'
18+
import { echo, sleep, parseArgv } from '../build/goods.js'
1919

2020
describe('goods', () => {
2121
function zx(script) {
@@ -32,18 +32,6 @@ describe('goods', () => {
3232
assert.match((await p).stdout, /Answer is foo/)
3333
})
3434

35-
test('globby() works', async () => {
36-
assert.equal(globby, glob)
37-
assert.deepEqual(await globby('*.md'), ['README.md'])
38-
})
39-
40-
test('fetch() works', async () => {
41-
assert.match(
42-
await fetch('https://medv.io').then((res) => res.text()),
43-
/Anton Medvedev/
44-
)
45-
})
46-
4735
test('echo() works', async () => {
4836
const log = console.log
4937
let stdout = ''
@@ -61,33 +49,6 @@ describe('goods', () => {
6149
assert.match(stdout, /foo/)
6250
})
6351

64-
test('YAML works', async () => {
65-
assert.deepEqual(YAML.parse(YAML.stringify({ foo: 'bar' })), { foo: 'bar' })
66-
})
67-
68-
test('which() available', async () => {
69-
assert.equal(which.sync('npm'), await which('npm'))
70-
})
71-
72-
test('minimist available', async () => {
73-
assert.equal(typeof minimist, 'function')
74-
})
75-
76-
test('minimist works', async () => {
77-
assert.deepEqual(
78-
minimist(
79-
['--foo', 'bar', '-a', '5', '-a', '42', '--force', './some.file'],
80-
{ boolean: 'force' }
81-
),
82-
{
83-
a: [5, 42],
84-
foo: 'bar',
85-
force: true,
86-
_: ['./some.file'],
87-
}
88-
)
89-
})
90-
9152
test('sleep() works', async () => {
9253
const now = Date.now()
9354
await sleep(100)

test/package.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import assert from 'node:assert'
1616
import { test, describe, after, before } from 'node:test'
17-
import '../build/globals.js'
17+
import { $, within, path, glob } from '../build/index.js'
1818

1919
const __dirname = new URL('.', import.meta.url).pathname
2020
const root = path.resolve(__dirname, '..')

test/vendor.test.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import assert from 'node:assert'
16+
import { test, describe } from 'node:test'
17+
import {
18+
YAML,
19+
minimist,
20+
which,
21+
glob,
22+
nodeFetch as fetch,
23+
} from '../build/vendor.js'
24+
25+
describe('vendor API', () => {
26+
test('YAML.parse', () => {
27+
assert.deepEqual(YAML.parse('a: b\n'), { a: 'b' })
28+
})
29+
30+
test('YAML.stringify', () => {
31+
assert.equal(YAML.stringify({ a: 'b' }), 'a: b\n')
32+
})
33+
34+
test('globby() works', async () => {
35+
assert.deepEqual(await glob('*.md'), ['README.md'])
36+
})
37+
38+
test('fetch() works', async () => {
39+
assert.match(
40+
await fetch('https://medv.io').then((res) => res.text()),
41+
/Anton Medvedev/
42+
)
43+
})
44+
45+
test('which() available', async () => {
46+
assert.equal(which.sync('npm'), await which('npm'))
47+
})
48+
49+
test('minimist available', async () => {
50+
assert.equal(typeof minimist, 'function')
51+
})
52+
53+
test('minimist works', async () => {
54+
assert.deepEqual(
55+
minimist(
56+
['--foo', 'bar', '-a', '5', '-a', '42', '--force', './some.file'],
57+
{ boolean: 'force' }
58+
),
59+
{
60+
a: [5, 42],
61+
foo: 'bar',
62+
force: true,
63+
_: ['./some.file'],
64+
}
65+
)
66+
})
67+
})

test/yaml.test.js

-27
This file was deleted.

0 commit comments

Comments
 (0)