-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
docs(test-runner-module-mocking): ✏️ test-moduleMocking details on plugin import order #2642
Open
movrack
wants to merge
1
commit into
modernweb-dev:master
Choose a base branch
from
movrack:feat/test-runner-module-mocking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@web/test-runner-module-mocking': patch | ||
--- | ||
|
||
Add docs on plugin import order to avoid conflict with other plugins | ||
+ refactoring | ||
+ using sinon dependency for new test using stub |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -37,15 +37,37 @@ export function moduleMockingPlugin(): Plugin { | |
const namedExports = exports.map(e => e.n).filter(n => n !== 'default'); | ||
const hasDefaultExport = exports.some(e => e.n === 'default'); | ||
|
||
body = ` | ||
body = generateInterceptedModuleBody(body, url, namedExports, hasDefaultExport); | ||
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. There is no real reason to extract this in a separate method, right? Can you revert this change? |
||
} | ||
} catch (error) { | ||
// Server side errors might contain ANSI color escape sequences. | ||
// These sequences are not readable in a browser's console, so we strip them. | ||
const errorMessage = stripColor((error as Error).message).replace(/'/g, "\\'"); | ||
body = `export const __wtr_error__ = '${errorMessage}';`; | ||
} | ||
|
||
return body ? { body, type: 'text/javascript' } : undefined; | ||
}, | ||
|
||
resolveImport({ source, context }) { | ||
if (context.path === '/__intercept-module__') { | ||
absolutePaths.push(source); | ||
} else if (absolutePaths.includes(source)) { | ||
return `${source}?intercept-module`; | ||
} | ||
return undefined; | ||
}, | ||
}; | ||
|
||
function generateInterceptedModuleBody(body: any, url: URL, namedExports: string[], hasDefaultExport: boolean) { | ||
body = ` | ||
import * as original from '${url.pathname}'; | ||
const newOriginal = {...original}; | ||
|
||
${namedExports.map(x => `export let ${x} = newOriginal['${x}'];`).join('\n')} | ||
|
||
${ | ||
hasDefaultExport | ||
? ` | ||
${hasDefaultExport | ||
? ` | ||
function computeDefault() { | ||
if(typeof newOriginal.default === 'function'){ | ||
return (...args) => newOriginal.default.call(undefined, ...args); | ||
|
@@ -55,8 +77,7 @@ export function moduleMockingPlugin(): Plugin { | |
|
||
export default computeDefault() | ||
` | ||
: '' | ||
} | ||
: ''} | ||
|
||
export const __wtr_intercepted_module__ = new Proxy(newOriginal, { | ||
set: function(obj, prop, value) { | ||
|
@@ -69,24 +90,6 @@ export function moduleMockingPlugin(): Plugin { | |
}, | ||
}); | ||
`; | ||
} | ||
} catch (error) { | ||
// Server side errors might contain ANSI color escape sequences. | ||
// These sequences are not readable in a browser's console, so we strip them. | ||
const errorMessage = stripColor((error as Error).message).replace(/'/g, "\\'"); | ||
body = `export const __wtr_error__ = '${errorMessage}';`; | ||
} | ||
|
||
return body ? { body, type: 'text/javascript' } : undefined; | ||
}, | ||
|
||
resolveImport({ source, context }) { | ||
if (context.path === '/__intercept-module__') { | ||
absolutePaths.push(source); | ||
} else if (absolutePaths.includes(source)) { | ||
return `${source}?intercept-module`; | ||
} | ||
return undefined; | ||
}, | ||
}; | ||
return body; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ule-mocking/test/fixtures/server-relative-with-stub/fixture/src/flow/libs/time-library.js
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,3 @@ | ||
export function getCurrentHour() { | ||
return 2; | ||
} |
9 changes: 9 additions & 0 deletions
9
...ule-mocking/test/fixtures/server-relative-with-stub/fixture/src/flow/step/getTimeOfDay.js
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,9 @@ | ||
import { getCurrentHour } from '../libs/time-library.js'; | ||
|
||
export function getTimeOfDay() { | ||
const hour = getCurrentHour(); | ||
if (hour < 6 || hour > 18) { | ||
return 'night'; | ||
} | ||
return 'day'; | ||
} |
33 changes: 33 additions & 0 deletions
33
...t-runner-module-mocking/test/fixtures/server-relative-with-stub/test/step/browser-test.js
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,33 @@ | ||
import { expect } from '../../../chai.js'; | ||
import sinon from 'sinon' | ||
import { importMockable } from '../../../../../browser/index.js'; | ||
|
||
const path = new URL(import.meta.resolve('../../fixture/src/flow/libs/time-library.js')).pathname; | ||
const timeLibrary = await importMockable(path); | ||
const { getTimeOfDay } = await import('../../fixture/src/flow/step/getTimeOfDay.js'); | ||
|
||
let backup; | ||
it('can run a module normally', () => { | ||
backup = timeLibrary.getCurrentHour; | ||
const timeOfDay = getTimeOfDay(); | ||
expect(timeOfDay).to.equal('night'); | ||
}); | ||
|
||
it('can intercept a module', () => { | ||
timeLibrary.getCurrentHour = () => 12; | ||
const timeOfDay = getTimeOfDay(); | ||
expect(timeOfDay).to.equal('day'); | ||
}); | ||
|
||
it('can restore an intercepted module', () => { | ||
timeLibrary.getCurrentHour = backup; | ||
const timeOfDay = getTimeOfDay(); | ||
expect(timeOfDay).to.equal('night'); | ||
}); | ||
|
||
it('can have stub applied on intercepted module', () => { | ||
const stub = sinon.stub(timeLibrary, 'getCurrentHour').returns(15) | ||
const timeOfDay = getTimeOfDay(); | ||
expect(timeOfDay).to.equal('day'); | ||
expect(stub.calledOnce).to.be.true; | ||
}); |
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.