-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: rewrite tests #1081
Merged
+1,028
−367
Merged
chore: rewrite tests #1081
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b6b7c21
remove prop tracking on event wrapper
ph-fritsche 2edd909
remove redundant declaration
ph-fritsche e60610c
remove prop tracking from listeners
ph-fritsche a457909
import cjs modules per interop
ph-fritsche 4514f1b
remove dependency on global jest object
ph-fritsche 69daeb1
fix tsconfig
ph-fritsche 4fc80d7
lint
ph-fritsche b14ddd0
configure testenv types
ph-fritsche bc2a8b6
eslintignore
ph-fritsche 192293d
lint
ph-fritsche 3fae0c9
decrease severity for `@typescript-eslint/no-unsafe-argument`
ph-fritsche 86ebadb
update `toHaveProperty` assertion
ph-fritsche cffa522
remove snapshot serializer `jest-serializer-path`
ph-fritsche 0d2810e
update jest-environment import
ph-fritsche cfd3ec9
do not access Window properties as globals
ph-fritsche 98d0c46
remove dependency on jest environment
ph-fritsche ae72570
add test environments and scripts
ph-fritsche fab7d87
lint
ph-fritsche bb007ff
disable coverage threshold
ph-fritsche a2536a1
fix setup:env script
ph-fritsche 6074c18
update test script
ph-fritsche a467120
add missing dev dependencies
ph-fritsche f9c1bc9
run conductors consecutively
ph-fritsche 11c6b16
close servers instead of exit
ph-fritsche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/coverage | ||
/dist | ||
/node_modules | ||
/testenv | ||
/scripts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type": "module"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
import { createBundleBuilder } from '@ph.fritsche/toolbox/dist/builder/index.js' | ||
import { spawn } from 'child_process' | ||
|
||
const dirname = path.dirname(new URL(import.meta.url).pathname) | ||
const indexDirLib = path.join(dirname, '../testenv/libs') | ||
const indexDirEnv = path.join(dirname, '../testenv') | ||
|
||
const ignoreEnv = ['node.js', 'jest.js'] | ||
|
||
const cmd = process.argv[2] | ||
const names = process.argv.length > 3 ? process.argv.slice(3) : undefined | ||
|
||
if (cmd === 'install-lib') { | ||
await Promise.all( | ||
(await getLibDirs(names)) | ||
.map(([name, dir]) => installLib(name, dir)) | ||
) | ||
} else if (cmd === 'bundle-lib') { | ||
await Promise.all( | ||
(await getLibDirs(names)) | ||
.map(([name, dir]) => buildLib(name, dir)) | ||
) | ||
} else if (cmd === 'bundle-env') { | ||
await Promise.all( | ||
(await getEnvFiles(names)) | ||
.map(([name, file]) => buildEnv(name, file)) | ||
) | ||
} else if (!cmd) { | ||
await Promise.all([ | ||
...(await getLibDirs()).map(([name, dir]) => installLib(name, dir).then(() => buildLib(name, dir))), | ||
...(await getEnvFiles()).map(([name, file]) => buildEnv(name, file)), | ||
]) | ||
} | ||
|
||
async function getLibDirs(names) { | ||
names ??= (await fs.readdir(indexDirLib)).filter(n => !n.startsWith('.')) | ||
|
||
return await Promise.all(names.map(name => { | ||
const dir = `${indexDirLib}/${name}` | ||
|
||
return fs.stat(`${dir}/index.js`).then( | ||
() => [name, dir], | ||
() => {throw new Error(`${dir}/index.js could not be found.`)} | ||
) | ||
})) | ||
} | ||
|
||
async function getEnvFiles(names) { | ||
names ??= (await fs.readdir(indexDirEnv)) | ||
.filter(n => /^\w+\.js$/.test(n)) | ||
.filter(n => !ignoreEnv.includes(n)) | ||
.map(f => f.slice(0, f.length - 3)) | ||
|
||
return await Promise.all(names.map(async name => { | ||
const file = `${indexDirEnv}/${name}.js` | ||
|
||
return fs.stat(file).then( | ||
() => [name, file], | ||
() => { throw new Error(`${file} could not be found.`)} | ||
) | ||
})) | ||
} | ||
|
||
async function installLib(name, dir) { | ||
return new Promise((res, rej) => { | ||
const child = spawn('npm', ['i'], {cwd: dir}) | ||
|
||
process.stdout.write(`Installing library "${name}" at ${dir}\n`) | ||
|
||
child.on('error', e => { | ||
process.stdout.write(`${e.stack ?? String(e)}\n`) | ||
}) | ||
child.on('exit', (code, signal) => { | ||
(code || signal ? rej(code) : res()) | ||
}) | ||
}) | ||
} | ||
|
||
async function buildLib(name, dir) { | ||
const { globals } = JSON.parse(await fs.readFile(`${dir}/package.json`)) | ||
|
||
process.stdout.write(`Bundling library "${name}" at ${dir}/index.js\n`) | ||
|
||
const builder = createBundleBuilder({ | ||
basePath: `${dir}/`, | ||
globals, | ||
}) | ||
builder.inputFiles.set(`${dir}/index.js`, undefined) | ||
|
||
builder.emitter.addListener('complete', e => { | ||
const content = String(e.outputFiles.get('index.js')?.content) | ||
fs.writeFile(`${dir}/index.bundle.js`, content) | ||
.then(() => process.stdout.write([ | ||
'<<<', | ||
`Wrote ${dir}/index.bundle.js`, | ||
`[${content.length} bytes]`, | ||
...((globals && Object.keys(globals).length) | ||
? [ | ||
` Depending on:`, | ||
...Object.entries(globals).map(([module, name]) => ` ${name} => ${module}`), | ||
] | ||
: []), | ||
'>>>', | ||
'', | ||
].join('\n'))) | ||
}) | ||
|
||
builder.build() | ||
} | ||
|
||
async function buildEnv(name, file) { | ||
process.stdout.write(`Bundling environment "${name}" at ${file}\n`) | ||
|
||
const builder = createBundleBuilder({ | ||
basePath: `${indexDirEnv}/`, | ||
}) | ||
const basename = path.basename(file, '.js') | ||
builder.inputFiles.set(file, undefined) | ||
|
||
builder.emitter.addListener('complete', e => { | ||
const content = String(e.outputFiles.get(`${basename}.js`)?.content) | ||
fs.writeFile(`${indexDirEnv}/${basename}.bundle.js`, content) | ||
.then(() => process.stdout.write([ | ||
'<<<', | ||
`Wrote ${indexDirEnv}/${basename}.bundle.js`, | ||
`[${content.length} bytes]`, | ||
'>>>', | ||
'', | ||
].join('\n'))) | ||
}) | ||
|
||
builder.build() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { createProjectBuildProvider, serveDir, serveToolboxRunner } from '@ph.fritsche/toolbox' | ||
import { NodeTestConductor } from '@ph.fritsche/toolbox/dist/conductor/NodeTestConductor.js' | ||
import { ChromeTestConductor } from '@ph.fritsche/toolbox/dist/conductor/ChromeTestConductor.js' | ||
import { ConsoleReporter } from '@ph.fritsche/toolbox/dist/reporter/ConsoleReporter.js' | ||
import { ReporterServer } from '@ph.fritsche/toolbox/dist/reporter/ReporterServer.js' | ||
import { TestRunStack } from '@ph.fritsche/toolbox/dist/reporter/TestRunStack.js' | ||
|
||
import IstanbulLibCoverage from 'istanbul-lib-coverage' | ||
import IstanbulLibReport from 'istanbul-lib-report' | ||
import IstanbulLibSourceMaps from 'istanbul-lib-source-maps' | ||
import IstanbulReports from 'istanbul-reports' | ||
|
||
const tsConfigFile = './tests/tsconfig.json' | ||
|
||
const toolbox = await serveToolboxRunner() | ||
const env = await serveDir('testenv') | ||
|
||
const { buildProvider, fileProvider, fileServer, onBuildDone } = createProjectBuildProvider([ | ||
'src', | ||
'tests', | ||
], { | ||
tsConfigFile, | ||
globals: { | ||
'@testing-library/dom': 'DomTestingLibrary', | ||
'@testing-library/react': 'ReactTestingLibrary', | ||
'react': 'React', | ||
'react-dom': 'ReactDom', | ||
} | ||
}) | ||
|
||
for (const { builder } of buildProvider.builders) { | ||
builder.emitter.addListener('start', ({ type, buildId, inputFiles }) => console.log(builder.id, { type, buildId, inputFiles: inputFiles.size })) | ||
builder.emitter.addListener('complete', ({ type, buildId, inputFiles, outputFiles }) => console.log(builder.id, { type, buildId, inputFiles: inputFiles.size, outputFiles: outputFiles.size })) | ||
builder.emitter.addListener('error', ({ type, buildId, error }) => console.log(builder.id, { type, buildId, error })) | ||
builder.emitter.addListener('done', ({ type, buildId }) => console.log(builder.id, { type, buildId })) | ||
} | ||
buildProvider.getBuilder('dependencies').builder.emitter.addListener('start', ({inputFiles}) => console.log('dependencies', inputFiles.keys())) | ||
|
||
const filter = (f) => f.startsWith('tests') | ||
&& /(?<!\.json)\.js$/.test(f) | ||
&& !/\/_.+(?<!\.test)\.[jt]sx?$/.test(f) | ||
|
||
const reporterServer = new ReporterServer() | ||
await reporterServer.registerFileServer(toolbox.server) | ||
await reporterServer.registerFileServer(env.server) | ||
await reporterServer.registerFileServer(fileServer) | ||
|
||
const consoleReporter = new ConsoleReporter() | ||
consoleReporter.config.result = !!process.env.CI | ||
consoleReporter.connect(reporterServer) | ||
|
||
const conductors = [ | ||
new ChromeTestConductor(reporterServer, toolbox.url, 'Chrome, DTL8, React18', [ | ||
{server: env.url, paths: ['browser.bundle.js']}, | ||
{server: env.url, paths: [ | ||
'libs/dom8/index.bundle.js', | ||
'libs/react18/index.bundle.js', | ||
]} | ||
]), | ||
new NodeTestConductor(reporterServer, toolbox.url, 'Node, DTL8, React18', [ | ||
{server: new URL(`file://${env.provider.origin}`), paths: ['node.js']}, | ||
{server: env.url, paths: [ | ||
'libs/dom8/index.bundle.js', | ||
'libs/react18/index.bundle.js', | ||
]}, | ||
]), | ||
] | ||
|
||
if (process.env.CI) { | ||
conductors.push( | ||
new NodeTestConductor(reporterServer, toolbox.url, 'Node, DTL8, React17', [ | ||
{server: new URL(`file://${env.provider.origin}`), paths: ['node.js'] }, | ||
{server: env.url, paths: [ | ||
'libs/dom8/index.bundle.js', | ||
'libs/react17/index.bundle.js', | ||
]}, | ||
]) | ||
) | ||
} | ||
|
||
onBuildDone(async () => { | ||
const files = { | ||
server: await fileServer.url, | ||
paths: Array.from(fileProvider.files.keys()).filter(filter), | ||
} | ||
const runs = conductors.map(c => c.createTestRun(files)) | ||
const stack = new TestRunStack(runs.map(r => r.run)) | ||
|
||
for (const r of runs) { | ||
await r.exec() | ||
} | ||
|
||
await stack.then() | ||
|
||
const coverageMap = IstanbulLibCoverage.createCoverageMap() | ||
for (const run of stack.runs) { | ||
for (const coverage of run.coverage.values()) { | ||
coverageMap.merge(coverage) | ||
} | ||
} | ||
|
||
const sourceStore = IstanbulLibSourceMaps.createSourceMapStore() | ||
const reportContext = IstanbulLibReport.createContext({ | ||
coverageMap: await sourceStore.transformCoverage(coverageMap), | ||
dir: fileProvider.origin, | ||
sourceFinder: sourceStore.sourceFinder, | ||
defaultSummarizer: 'nested', | ||
watermarks: { | ||
branches: [80, 100], | ||
functions: [80, 100], | ||
lines: [80, 100], | ||
statements: [80, 100], | ||
}, | ||
}) | ||
|
||
IstanbulReports.create('text').execute(reportContext) | ||
|
||
if (process.env.CI) { | ||
toolbox.server.close() | ||
env.server.close() | ||
fileServer.close() | ||
buildProvider.close() | ||
reporterServer.close() | ||
conductors.forEach(c => c.close()) | ||
} | ||
}) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ | ||
|
||
import def, * as named from '@testing-library/dom' | ||
|
||
export default def ?? named |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ | ||
|
||
import def, * as named from '@testing-library/dom/dist/event-map.js' | ||
|
||
export default def ?? named |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ | ||
|
||
import def, * as named from '@testing-library/dom/dist/helpers.js' | ||
|
||
export default def ?? named |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to cause some problems since there is no default export from
@testing-library/dom
. I haven't seen this kind of approach before, just curious, what is the reason for writing it this way?