-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: support test projects #420
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
Merged
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6223563
feat: init projects config
9aoy 2c7175e
fix: merge conflict
9aoy 8a6f5d6
Merge branch 'main' of https://github.com/web-infra-dev/rstest into p…
9aoy d2b564a
feat: support test projects
9aoy dc73f68
fix: setupFiles
9aoy 434eaaf
fix: merge conflict
9aoy e230567
feat: support glob pattern
9aoy 3a42499
fix: should throw error when project not found
9aoy bf5a5e6
feat: should run test failed when test file not found
9aoy 3ac39e0
Merge branch 'main' of https://github.com/web-infra-dev/rstest into p…
9aoy a0ad3f0
fix: merge conflict
9aoy 83105ff
fix: tests
9aoy afc83ef
perf: reuse pool
9aoy 1855377
fix: merge conflict
9aoy 32a2f1c
fix: merge conflict
9aoy 146adf6
fix: lint
9aoy 446a423
fix: formatEnvironmentName
9aoy b2cb773
fix: merge conflict
9aoy 94d1279
fix: project rootPath
9aoy 1d7d7fc
fix: merge conflict
9aoy 3370a75
fix: should not inherit root config by default
9aoy 1841fe4
docs: add projects config
9aoy 32ad196
fix: merge conflict
9aoy 863ede7
fix: update `no test files found` log
9aoy cc74f9e
fix: update log
9aoy 3ca6e0b
Update website/docs/zh/config/test/projects.mdx
9aoy e7a861f
Update website/docs/en/config/test/projects.mdx
fi3ework 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,170 @@ | ||||||||||||||||
import { existsSync, readFileSync } from 'node:fs'; | ||||||||||||||||
import type { LoadConfigOptions } from '@rsbuild/core'; | ||||||||||||||||
import { basename, resolve } from 'pathe'; | ||||||||||||||||
import { isDynamicPattern } from 'tinyglobby'; | ||||||||||||||||
import { loadConfig } from '../config'; | ||||||||||||||||
import type { Project, RstestConfig } from '../types'; | ||||||||||||||||
import { castArray, getAbsolutePath } from '../utils/helper'; | ||||||||||||||||
|
||||||||||||||||
export type CommonOptions = { | ||||||||||||||||
root?: string; | ||||||||||||||||
config?: string; | ||||||||||||||||
configLoader?: LoadConfigOptions['loader']; | ||||||||||||||||
globals?: boolean; | ||||||||||||||||
isolate?: boolean; | ||||||||||||||||
include?: string[]; | ||||||||||||||||
exclude?: string[]; | ||||||||||||||||
passWithNoTests?: boolean; | ||||||||||||||||
printConsoleTrace?: boolean; | ||||||||||||||||
disableConsoleIntercept?: boolean; | ||||||||||||||||
update?: boolean; | ||||||||||||||||
testNamePattern?: RegExp | string; | ||||||||||||||||
testTimeout?: number; | ||||||||||||||||
hookTimeout?: number; | ||||||||||||||||
testEnvironment?: string; | ||||||||||||||||
clearMocks?: boolean; | ||||||||||||||||
resetMocks?: boolean; | ||||||||||||||||
restoreMocks?: boolean; | ||||||||||||||||
unstubGlobals?: boolean; | ||||||||||||||||
unstubEnvs?: boolean; | ||||||||||||||||
retry?: number; | ||||||||||||||||
maxConcurrency?: number; | ||||||||||||||||
slowTestThreshold?: number; | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
async function resolveConfig( | ||||||||||||||||
options: CommonOptions & Required<Pick<CommonOptions, 'root'>>, | ||||||||||||||||
): Promise<{ | ||||||||||||||||
config: RstestConfig; | ||||||||||||||||
configFilePath: string | null; | ||||||||||||||||
}> { | ||||||||||||||||
const { content: config, filePath: configFilePath } = await loadConfig({ | ||||||||||||||||
cwd: options.root, | ||||||||||||||||
path: options.config, | ||||||||||||||||
configLoader: options.configLoader, | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
const keys: (keyof CommonOptions & keyof RstestConfig)[] = [ | ||||||||||||||||
'root', | ||||||||||||||||
'globals', | ||||||||||||||||
'isolate', | ||||||||||||||||
'passWithNoTests', | ||||||||||||||||
'update', | ||||||||||||||||
'testNamePattern', | ||||||||||||||||
'testTimeout', | ||||||||||||||||
'hookTimeout', | ||||||||||||||||
'clearMocks', | ||||||||||||||||
'resetMocks', | ||||||||||||||||
'restoreMocks', | ||||||||||||||||
'unstubEnvs', | ||||||||||||||||
'unstubGlobals', | ||||||||||||||||
'retry', | ||||||||||||||||
'slowTestThreshold', | ||||||||||||||||
'maxConcurrency', | ||||||||||||||||
'printConsoleTrace', | ||||||||||||||||
'disableConsoleIntercept', | ||||||||||||||||
'testEnvironment', | ||||||||||||||||
]; | ||||||||||||||||
for (const key of keys) { | ||||||||||||||||
if (options[key] !== undefined) { | ||||||||||||||||
(config[key] as any) = options[key]; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if (options.exclude) { | ||||||||||||||||
config.exclude = castArray(options.exclude); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if (options.include) { | ||||||||||||||||
config.include = castArray(options.include); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
return { | ||||||||||||||||
config, | ||||||||||||||||
configFilePath, | ||||||||||||||||
}; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
export async function resolveProjects({ | ||||||||||||||||
config, | ||||||||||||||||
root, | ||||||||||||||||
options, | ||||||||||||||||
}: { | ||||||||||||||||
config: RstestConfig; | ||||||||||||||||
root: string; | ||||||||||||||||
options: CommonOptions; | ||||||||||||||||
}): Promise<Project[]> { | ||||||||||||||||
const projects: Project[] = []; | ||||||||||||||||
|
||||||||||||||||
const getDefaultProjectName = (dir: string) => { | ||||||||||||||||
const pkgJsonPath = resolve(dir, 'package.json'); | ||||||||||||||||
const name = existsSync(pkgJsonPath) | ||||||||||||||||
? JSON.parse(readFileSync(pkgJsonPath, 'utf-8')).name | ||||||||||||||||
: ''; | ||||||||||||||||
|
||||||||||||||||
if (typeof name !== 'string' || !name) { | ||||||||||||||||
return basename(dir); | ||||||||||||||||
} | ||||||||||||||||
return name; | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
const names = new Set<string>(); | ||||||||||||||||
|
||||||||||||||||
for (const project of config.projects || []) { | ||||||||||||||||
const projectStr = project.replace('<rootDir>', root); | ||||||||||||||||
|
||||||||||||||||
if (isDynamicPattern(projectStr)) { | ||||||||||||||||
// TODO | ||||||||||||||||
throw `Dynamic project pattern (${project}) is not supported. Please use static paths.`; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
const absolutePath = getAbsolutePath(root, projectStr); | ||||||||||||||||
|
||||||||||||||||
const { config, configFilePath } = await resolveConfig({ | ||||||||||||||||
...options, | ||||||||||||||||
root: absolutePath, | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
config.name ??= getDefaultProjectName(absolutePath); | ||||||||||||||||
|
||||||||||||||||
projects.push({ | ||||||||||||||||
config, | ||||||||||||||||
configFilePath, | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
if (names.has(config.name)) { | ||||||||||||||||
const conflictProjects = projects.filter( | ||||||||||||||||
(p) => p.config.name === config.name, | ||||||||||||||||
); | ||||||||||||||||
throw `Project name "${config.name}" is already used. Please ensure all projects have unique names. | ||||||||||||||||
Conflicting projects: | ||||||||||||||||
${conflictProjects.map((p) => `- ${p.configFilePath || p.config.root}`).join('\n')} | ||||||||||||||||
`; | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing a string instead of an Error object is not a best practice. Consider using
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
names.add(config.name); | ||||||||||||||||
} | ||||||||||||||||
return projects; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
export async function initCli(options: CommonOptions): Promise<{ | ||||||||||||||||
config: RstestConfig; | ||||||||||||||||
configFilePath: string | null; | ||||||||||||||||
projects: Project[]; | ||||||||||||||||
}> { | ||||||||||||||||
const cwd = process.cwd(); | ||||||||||||||||
const root = options.root ? getAbsolutePath(cwd, options.root) : cwd; | ||||||||||||||||
|
||||||||||||||||
const { config, configFilePath } = await resolveConfig({ | ||||||||||||||||
...options, | ||||||||||||||||
root, | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
const projects = await resolveProjects({ config, root, options }); | ||||||||||||||||
|
||||||||||||||||
return { | ||||||||||||||||
config, | ||||||||||||||||
configFilePath, | ||||||||||||||||
projects, | ||||||||||||||||
}; | ||||||||||||||||
} |
This file contains hidden or 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
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.
Throwing a string instead of an Error object is not a best practice. Consider using
throw new Error()
for better error handling and stack traces.Copilot uses AI. Check for mistakes.