Commit f30a9d0

Anton Golub <antongolub@antongolub.com>
2024-12-25 20:48:26
test: remove zx globals from unit tests (#1039)
closes #1036
1 parent 131e6c9
test/all.test.js
@@ -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'
test/cli.test.js
@@ -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,
@@ -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
test/global.test.js
@@ -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('/')
test/goods.test.js
@@ -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) {
@@ -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 = ''
@@ -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)
test/package.test.js
@@ -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, '..')
test/vendor.test.js
@@ -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'],
+      }
+    )
+  })
+})
test/yaml.test.js
@@ -1,27 +0,0 @@
-// 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 } from '../build/vendor.js'
-
-describe('YAML', () => {
-  test('YAML.parse', () => {
-    assert.deepEqual(YAML.parse('a: b\n'), { a: 'b' })
-  })
-
-  test('YAML.stringify', () => {
-    assert.equal(YAML.stringify({ a: 'b' }), 'a: b\n')
-  })
-})