Skip to content

Commit

Permalink
test: remove zx globals from unit tests (#1039)
Browse files Browse the repository at this point in the history
closes #1036
  • Loading branch information
antongolub authored Dec 25, 2024
1 parent 131e6c9 commit f30a9d0
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 73 deletions.
4 changes: 2 additions & 2 deletions test/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import './cli.test.js'
import './core.test.js'
import './deps.test.js'
import './error.test.ts'
import './export.test.js'
import './global.test.js'
import './goods.test.js'
import './index.test.js'
import './package.test.js'
import './util.test.js'
import './yaml.test.js'
import './export.test.js'
import './vendor.test.js'
2 changes: 1 addition & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { test, describe, before, after } from 'node:test'
import { fileURLToPath } from 'node:url'
import net from 'node:net'
import getPort from 'get-port'
import '../build/globals.js'
import {
argv,
importPath,
Expand All @@ -32,6 +31,7 @@ import {
transformMarkdown,
writeAndImport,
} from '../build/cli.js'
import { $, path, fs, tmpfile, tmpdir } from '../build/index.js'

const __filename = fileURLToPath(import.meta.url)
const spawn = $.spawn
Expand Down
8 changes: 7 additions & 1 deletion test/global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
// limitations under the License.

import assert from 'node:assert'
import { test, describe } from 'node:test'
import { test, describe, after } from 'node:test'
import '../build/globals.js'
import * as index from '../build/index.js'

describe('global', () => {
after(() => {
for (const key of Object.keys(index)) {
delete global[key]
}
})

test('global cd()', async () => {
const cwd = (await $`pwd`).toString().trim()
cd('/')
Expand Down
43 changes: 2 additions & 41 deletions test/goods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import chalk from 'chalk'
import assert from 'node:assert'
import { test, describe } from 'node:test'
import '../build/globals.js'
import { $, chalk } from '../build/index.js'
import { echo, sleep, parseArgv } from '../build/goods.js'

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

test('globby() works', async () => {
assert.equal(globby, glob)
assert.deepEqual(await globby('*.md'), ['README.md'])
})

test('fetch() works', async () => {
assert.match(
await fetch('https://medv.io').then((res) => res.text()),
/Anton Medvedev/
)
})

test('echo() works', async () => {
const log = console.log
let stdout = ''
Expand All @@ -61,33 +49,6 @@ describe('goods', () => {
assert.match(stdout, /foo/)
})

test('YAML works', async () => {
assert.deepEqual(YAML.parse(YAML.stringify({ foo: 'bar' })), { foo: 'bar' })
})

test('which() available', async () => {
assert.equal(which.sync('npm'), await which('npm'))
})

test('minimist available', async () => {
assert.equal(typeof minimist, 'function')
})

test('minimist works', async () => {
assert.deepEqual(
minimist(
['--foo', 'bar', '-a', '5', '-a', '42', '--force', './some.file'],
{ boolean: 'force' }
),
{
a: [5, 42],
foo: 'bar',
force: true,
_: ['./some.file'],
}
)
})

test('sleep() works', async () => {
const now = Date.now()
await sleep(100)
Expand Down
2 changes: 1 addition & 1 deletion test/package.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import assert from 'node:assert'
import { test, describe, after, before } from 'node:test'
import '../build/globals.js'
import { $, within, path, glob } from '../build/index.js'

const __dirname = new URL('.', import.meta.url).pathname
const root = path.resolve(__dirname, '..')
Expand Down
67 changes: 67 additions & 0 deletions test/vendor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import assert from 'node:assert'
import { test, describe } from 'node:test'
import {
YAML,
minimist,
which,
glob,
nodeFetch as fetch,
} from '../build/vendor.js'

describe('vendor API', () => {
test('YAML.parse', () => {
assert.deepEqual(YAML.parse('a: b\n'), { a: 'b' })
})

test('YAML.stringify', () => {
assert.equal(YAML.stringify({ a: 'b' }), 'a: b\n')
})

test('globby() works', async () => {
assert.deepEqual(await glob('*.md'), ['README.md'])
})

test('fetch() works', async () => {
assert.match(
await fetch('https://medv.io').then((res) => res.text()),
/Anton Medvedev/
)
})

test('which() available', async () => {
assert.equal(which.sync('npm'), await which('npm'))
})

test('minimist available', async () => {
assert.equal(typeof minimist, 'function')
})

test('minimist works', async () => {
assert.deepEqual(
minimist(
['--foo', 'bar', '-a', '5', '-a', '42', '--force', './some.file'],
{ boolean: 'force' }
),
{
a: [5, 42],
foo: 'bar',
force: true,
_: ['./some.file'],
}
)
})
})
27 changes: 0 additions & 27 deletions test/yaml.test.js

This file was deleted.

0 comments on commit f30a9d0

Please sign in to comment.