-
-
Notifications
You must be signed in to change notification settings - Fork 12
fix: support use rstest global APIs in external modules #554
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'rstest-globals'; | ||
|
||
it('should run setup correctly', async () => { | ||
expect(process.env.A).toBe('A'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
beforeAll(() => { | ||
process.env.A = 'A'; | ||
}); | ||
|
||
describe('wrap test', () => { | ||
it('should run', () => { | ||
const fn = rs.fn(() => 'hello'); | ||
|
||
expect(fn()).toBe('hello'); | ||
expect('call it').toBe('call it'); | ||
}); | ||
|
||
it.todo('should not run', () => { | ||
expect(1 + 1).toBe(3); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"private": true, | ||
"name": "@rstest/tests-rstest-globals", | ||
"main": "index.js", | ||
"type": "module", | ||
"sideEffects": true, | ||
"version": "1.0.0", | ||
"devDependencies": { | ||
"@rstest/core": "workspace:*" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -14,11 +14,12 @@ import { loadModule } from './loadModule'; | |||||||
import { createForksRpcOptions, createRuntimeRpc } from './rpc'; | ||||||||
import { RstestSnapshotEnvironment } from './snapshot'; | ||||||||
|
||||||||
const getGlobalApi = (api: Rstest) => { | ||||||||
const registerGlobalApi = (api: Rstest) => { | ||||||||
return globalApis.reduce<{ | ||||||||
[key in keyof Rstest]?: Rstest[key]; | ||||||||
}>((apis, key) => { | ||||||||
apis[key] = api[key] as any; | ||||||||
// @ts-expect-error register to global | ||||||||
globalThis[key] = api[key] as any; | ||||||||
return apis; | ||||||||
}, {}); | ||||||||
}; | ||||||||
|
@@ -141,11 +142,14 @@ const preparePool = async ({ | |||||||
throw new Error(`Unknown test environment: ${testEnvironment}`); | ||||||||
} | ||||||||
|
||||||||
if (globals) { | ||||||||
registerGlobalApi(api); | ||||||||
} | ||||||||
|
||||||||
const rstestContext = { | ||||||||
global, | ||||||||
console: global.console, | ||||||||
Error, | ||||||||
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. The removal of the spread operator that conditionally included global APIs makes the code cleaner, but the context object now lacks the rstest APIs that were previously included when
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
...(globals ? getGlobalApi(api) : {}), | ||||||||
}; | ||||||||
|
||||||||
// @ts-expect-error | ||||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
The function returns an empty object but doesn't populate the
apis
accumulator. Since the function is now registering globals directly onglobalThis
, it should either return void or properly populate and return theapis
object.Copilot uses AI. Check for mistakes.