Skip to content

Commit

Permalink
formatting, and use consistent t.mockImport type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 18, 2024
1 parent 410f1ac commit eb060c7
Show file tree
Hide file tree
Showing 62 changed files with 941 additions and 925 deletions.
4 changes: 3 additions & 1 deletion src/asserts/test/index.ts
Expand Up @@ -143,7 +143,9 @@ t.test('same, notSame', t => {
t.ok(a.notSame({ a: 1, b: 2 }, { b: 2 }))
class Numberish {
foo: number
valueOf() { return 1 }
valueOf() {
return 1
}
constructor() {
this.foo = Math.random()
}
Expand Down
99 changes: 47 additions & 52 deletions src/config/test/index.ts
Expand Up @@ -15,19 +15,18 @@ t.test('basic tests', async t => {

t.test('reporter from env or config', t => {
t.test('default, with color', async t => {
const { TapConfig } = (await t.mockImport(
'../dist/esm/index.js',
{
'@tapjs/core': t.createMock(core, {
env: {
TAP: undefined,
TAP_REPORTER: undefined,
NO_COLOR: undefined,
TAP_COLOR: '1',
},
}),
}
)) as typeof import('../dist/esm/index.js')
const { TapConfig } = await t.mockImport<
typeof import('../dist/esm/index.js')
>('../dist/esm/index.js', {
'@tapjs/core': t.createMock(core, {
env: {
TAP: undefined,
TAP_REPORTER: undefined,
NO_COLOR: undefined,
TAP_COLOR: '1',
},
}),
})
const tc = await TapConfig.load()
t.equal(tc.get('reporter'), 'base')
t.equal(tc.get('reporter'), 'base', 'cache coverage')
Expand All @@ -44,10 +43,9 @@ t.test('reporter from env or config', t => {
},
}),
}
const { TapConfig } = (await t.mockImport(
'../dist/esm/index.js',
m
)) as typeof import('../dist/esm/index.js')
const { TapConfig } = await t.mockImport<
typeof import('../dist/esm/index.js')
>('../dist/esm/index.js', m)
t.equal((await TapConfig.load()).get('reporter'), 'tap')
})

Expand All @@ -61,16 +59,15 @@ t.test('reporter from env or config', t => {
},
}),
}
const { TapConfig } = (await t.mockImport(
'../dist/esm/index.js',
{
...m,
'../dist/esm/jack.js': await t.mockImport(
'../dist/esm/jack.js',
m
),
}
)) as typeof import('../dist/esm/index.js')
const { TapConfig } = await t.mockImport<
typeof import('../dist/esm/index.js')
>('../dist/esm/index.js', {
...m,
'../dist/esm/jack.js': await t.mockImport(
'../dist/esm/jack.js',
m
),
})
t.equal((await TapConfig.load()).get('reporter'), 'tap')
})

Expand All @@ -85,16 +82,15 @@ t.test('reporter from env or config', t => {
},
}),
}
const { TapConfig } = (await t.mockImport(
'../dist/esm/index.js',
{
...m,
'../dist/esm/jack.js': await t.mockImport(
'../dist/esm/jack.js',
m
),
}
)) as typeof import('../dist/esm/index.js')
const { TapConfig } = await t.mockImport<
typeof import('../dist/esm/index.js')
>('../dist/esm/index.js', {
...m,
'../dist/esm/jack.js': await t.mockImport(
'../dist/esm/jack.js',
m
),
})
t.equal((await TapConfig.load()).get('reporter'), 'base')
})

Expand Down Expand Up @@ -876,21 +872,20 @@ t.test('load file from env.TAP_RCFILE', async t => {
for (const cwd of ['git', 'taprc', 'pj']) {
process.chdir(dir + '/cwds/' + cwd)
t.test(`rcfile=${rcfile} cwd=${cwd}`, async t => {
const { TapConfig } = (await t.mockImport(
'../dist/esm/index.js',
{
'@tapjs/core': t.createMock(core, {
cwd: resolve(dir, 'cwds', cwd),
env: {
TAP: undefined,
TAP_REPORTER: undefined,
NO_COLOR: undefined,
TAP_COLOR: '1',
TAP_RCFILE: resolve(dir, rcfile),
},
}),
}
)) as typeof import('../dist/esm/index.js')
const { TapConfig } = await t.mockImport<
typeof import('../dist/esm/index.js')
>('../dist/esm/index.js', {
'@tapjs/core': t.createMock(core, {
cwd: resolve(dir, 'cwds', cwd),
env: {
TAP: undefined,
TAP_REPORTER: undefined,
NO_COLOR: undefined,
TAP_COLOR: '1',
TAP_RCFILE: resolve(dir, rcfile),
},
}),
})
const c = await TapConfig.load()
t.strictSame(c.get('reporter-arg'), rcfile.split('.'))
})
Expand Down
6 changes: 4 additions & 2 deletions src/config/test/jack.ts
@@ -1,7 +1,9 @@
import t from 'tap'
const { default: jack } = (await t.mockImport('../dist/esm/jack.js', {
const { default: jack } = await t.mockImport<
typeof import('../dist/esm/jack.js')
>('../dist/esm/jack.js', {
'../dist/esm/jobs.js': { jobs: 16 },
})) as typeof import('../dist/esm/jack.js')
})
t.matchSnapshot(jack.toJSON())
t.throws(() =>
//@ts-expect-error
Expand Down
38 changes: 18 additions & 20 deletions src/core/test/main-script.ts
Expand Up @@ -7,14 +7,13 @@ const __filename = fileURLToPath(import.meta.url)

t.equal(mainScript(), __filename)
t.test('use default in eval mode', async t => {
const { mainScript } = (await t.mockImport(
'../dist/esm/main-script.js',
{
'../dist/esm/proc.js': t.createMock(proc, {
proc: { _eval: 'some string' },
}),
}
)) as typeof import('../dist/esm/main-script.js')
const { mainScript } = await t.mockImport<
typeof import('../dist/esm/main-script.js')
>('../dist/esm/main-script.js', {
'../dist/esm/proc.js': t.createMock(proc, {
proc: { _eval: 'some string' },
}),
})
t.equal(mainScript('default'), 'default')
t.end()
})
Expand All @@ -25,23 +24,22 @@ t.test('use default in repl mode', async t => {
//@ts-ignore
delete globalThis.repl
})
const { mainScript } = (await t.mockImport(
'../dist/esm/main-script.js'
)) as typeof import('../dist/esm/main-script.js')
const { mainScript } = await t.mockImport<
typeof import('../dist/esm/main-script.js')
>('../dist/esm/main-script.js')
t.equal(mainScript('default'), 'default')
t.end()
})

t.test('use default if no argv[1] somehow', async t => {
const { mainScript } = (await t.mockImport(
'../dist/esm/main-script.js',
{
'../dist/esm/proc.js': t.createMock(proc, {
argv: [],
proc: { argv: [] },
}),
}
)) as typeof import('../dist/esm/main-script.js')
const { mainScript } = await t.mockImport<
typeof import('../dist/esm/main-script.js')
>('../dist/esm/main-script.js', {
'../dist/esm/proc.js': t.createMock(proc, {
argv: [],
proc: { argv: [] },
}),
})
t.equal(mainScript('default'), 'default')
t.end()
})
6 changes: 3 additions & 3 deletions src/core/test/proc.ts
Expand Up @@ -11,9 +11,9 @@ t.test('fallbacks', async t => {
value: undefined,
configurable: true,
})
const { proc, argv, cwd, env } = (await t.mockImport(
'../dist/esm/proc.js'
)) as typeof import('../dist/esm/proc.js')
const { proc, argv, cwd, env } = await t.mockImport<
typeof import('../dist/esm/proc.js')
>('../dist/esm/proc.js')
Object.defineProperty(globalThis, 'process', {
value: process_,
configurable: true,
Expand Down
6 changes: 4 additions & 2 deletions src/core/test/test-base.ts
Expand Up @@ -645,11 +645,13 @@ t.test('end stuff', t => {
})

t.test('fullname when mainScript not available', async t => {
const { TestBase } = (await t.mockImport('../dist/esm/index.js', {
const { TestBase } = await t.mockImport<
typeof import('../dist/esm/index.js')
>('../dist/esm/index.js', {
'../dist/esm/main-script.js': {
mainScript: (def: string) => def,
},
})) as typeof import('../dist/esm/index.js')
})
const tb = new TestBase({ name: 'full name' })
t.equal(tb.fullname, 'TAP > full name')
})
Expand Down
6 changes: 3 additions & 3 deletions src/esbuild-kit/test/index.ts
Expand Up @@ -43,9 +43,9 @@ t.test('set the configs from tap configs', async t => {
delete process.env.ESBK_DISABLE_CACHE
process.env.TAP_ESBK_TSCONFIG_PATH = 'some-path'
process.env.TAP_ESBK_DISABLE_CACHE = '1'
const { plugin } = (await t.mockImport(
'../dist/esm/index.js'
)) as typeof import('../dist/esm/index.js')
const { plugin } = await t.mockImport<
typeof import('../dist/esm/index.js')
>('../dist/esm/index.js')
const tb = new TestBase({ name: 'esbk env test' })
plugin(tb)
t.match(process.env, {
Expand Down
12 changes: 6 additions & 6 deletions src/mocha-globals/test/index.ts
Expand Up @@ -23,9 +23,9 @@ t.beforeEach(t =>

t.test('without global injection', async t => {
process.env.TAP_MOCHA_GLOBALS = '0'
const mg = (await t.mockImport(
'../dist/esm/index.js'
)) as typeof import('../dist/esm/index.js')
const mg = await t.mockImport<
typeof import('../dist/esm/index.js')
>('../dist/esm/index.js')
t.match(
mg,
{
Expand Down Expand Up @@ -70,9 +70,9 @@ t.test('without global injection', async t => {

t.test('with global injection', async t => {
process.env.TAP_MOCHA_GLOBALS = '1'
const mg = (await t.mockImport(
'../dist/esm/index.js'
)) as typeof import('../dist/esm/index.js')
const mg = await t.mockImport<
typeof import('../dist/esm/index.js')
>('../dist/esm/index.js')
t.match(
mg,
{
Expand Down
9 changes: 4 additions & 5 deletions src/mock/README.md
Expand Up @@ -26,12 +26,11 @@ t.test('handls stat failure by throwing', async t => {
})
}
// supply type param so that TS knows what it returns
const thingThatDoesStat = (await t.mockImport<
const thingThatDoesStat = await t.mockImport<
typeof import('../dist/my-statty-thing.js')
>(
'../dist/my-statty-thing.js',
{ 'node:fs': { statSync: mockStatSync } }
))
>('../dist/my-statty-thing.js', {
'node:fs': { statSync: mockStatSync },
})

t.throws(() => thingThatDoesStat('filename.txt'), {
message: 'expected error',
Expand Down
7 changes: 3 additions & 4 deletions src/mock/src/hooks-url-cjs.cts
@@ -1,7 +1,6 @@
import { pathToFileURL } from 'url'
const base = String(pathToFileURL(__filename))
const inDist = base.endsWith('dist/commonjs/hooks-url.js')
export const hooks = String(new URL(
inDist ? '../esm/hooks.mjs' : './hooks.mjs',
base
))
export const hooks = String(
new URL(inDist ? '../esm/hooks.mjs' : './hooks.mjs', base)
)
32 changes: 16 additions & 16 deletions src/mock/test/hooks-url.ts
@@ -1,24 +1,24 @@
import t from 'tap'
import { hooks } from '../dist/esm/hooks-url.js'
t.equal(String(hooks), String(new URL(
'../dist/esm/hooks.mjs',
import.meta.url
)))
t.equal(
String(hooks),
String(new URL('../dist/esm/hooks.mjs', import.meta.url))
)

import { hooks as hooksSrc } from '../src/hooks-url.js'
t.equal(String(hooksSrc), String(new URL(
'../src/hooks.mjs',
import.meta.url
)))
t.equal(
String(hooksSrc),
String(new URL('../src/hooks.mjs', import.meta.url))
)

import hooksCJSDist from '../dist/commonjs/hooks-url.js'
t.equal(String(hooksCJSDist.hooks), String(new URL(
'../dist/esm/hooks.mjs',
import.meta.url
)))
t.equal(
String(hooksCJSDist.hooks),
String(new URL('../dist/esm/hooks.mjs', import.meta.url))
)

import hooksCJSSrc from '../src/hooks-url-cjs.cjs'
t.equal(String(hooksCJSSrc.hooks), String(new URL(
'../src/hooks.mjs',
import.meta.url
)))
t.equal(
String(hooksCJSSrc.hooks),
String(new URL('../src/hooks.mjs', import.meta.url))
)
12 changes: 8 additions & 4 deletions src/mock/test/hooks.ts
Expand Up @@ -44,11 +44,13 @@ t.test('globalPreload', async t => {
}
}
let client: MockMockClient | undefined = undefined
const hooks = (await t.mockImport('../dist/esm/hooks.mjs', {
const hooks = await t.mockImport<
typeof import('../dist/esm/hooks.mjs')
>('../dist/esm/hooks.mjs', {
'../dist/esm/mock-service-client.js': {
MockServiceClient: MockMockClient,
},
})) as typeof import('../dist/esm/hooks.mjs')
})
t.equal(client, undefined, 'no client until initialized')
const message = (hook: 'load' | 'resolve') =>
'initialize() or globalPreload() must be run prior to ' +
Expand Down Expand Up @@ -136,11 +138,13 @@ t.test('initialize', async t => {
}
}
let client: MockMockClient | undefined = undefined
const hooks = (await t.mockImport('../dist/esm/hooks.mjs', {
const hooks = await t.mockImport<
typeof import('../dist/esm/hooks.mjs')
>('../dist/esm/hooks.mjs', {
'../dist/esm/mock-service-client.js': {
MockServiceClient: MockMockClient,
},
})) as typeof import('../dist/esm/hooks.mjs')
})
t.equal(client, undefined, 'no client until initialized')
const message = (hook: 'load' | 'resolve') =>
'initialize() or globalPreload() must be run prior to ' +
Expand Down

0 comments on commit eb060c7

Please sign in to comment.