-
Notifications
You must be signed in to change notification settings - Fork 9
Svelte component testing #19
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
Open
harshit-bs
wants to merge
12
commits into
nightwatchjs:main
Choose a base branch
from
harshit-bs:feat/svelte-ct
base: main
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
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0e3f46f
svelte component testing
harshit-bs 1eee1ef
lint fixes
harshit-bs 8273e0f
test fixes
harshit-bs 53d7a0b
CJS and ESM fix
harshit-bs 6c62619
code refactoring
harshit-bs 0a69a06
reset testPlugin
harshit-bs e60c8fd
code refactoring
harshit-bs 3237527
added types for svelte mount fn
harshit-bs 0551d6e
removed @types/nightwatch from dependencies
harshit-bs bb8aa74
imporscript fix for https://github.com/nightwatchjs/vite-plugin-night…
harshit-bs 17692ed
added svelte component tests in CI workflow
harshit-bs 3825cbb
updated node version
harshit-bs 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
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,189 @@ | ||
| const AssertionError = require('assertion-error'); | ||
|
|
||
| class NightwatchMountError extends AssertionError { | ||
| constructor(message) { | ||
| super(message); | ||
|
|
||
| this.name = 'NightwatchMountError'; | ||
| } | ||
| } | ||
|
|
||
| module.exports = class Command { | ||
| get pluginSettings() { | ||
| return this.client.settings['@nightwatch/svelte'] || {}; | ||
| } | ||
|
|
||
| getError(message) { | ||
| const err = new NightwatchMountError(message); | ||
|
|
||
| err.showTrace = false; | ||
| err.help = [ | ||
| 'run nightwatch with --devtools and --debug flags (Chrome only)', | ||
| 'investigate the error in the browser console' | ||
| ]; | ||
|
|
||
| return err; | ||
| } | ||
|
|
||
| async mountComponent(componentName, opts, isRetry = false) { | ||
| await this.api.execute(function (innerHTML) { | ||
| function onReady(fn) { | ||
| if (document.readyState === 'complete' || document.readyState === 'interactive') {setTimeout(fn)} else {document.addEventListener('DOMContentLoaded', fn)} | ||
| } | ||
| onReady(function() { | ||
| var scriptTag = Object.assign(document.createElement('script'), { | ||
| type: 'module', | ||
| innerHTML | ||
| }); | ||
| document.body.appendChild(scriptTag); | ||
| }); | ||
| }, [Command._buildScript(componentName, opts)], async (result) => { | ||
| if (result && (result.error instanceof Error) && !isRetry) { | ||
| return this.mountComponent(componentName, opts, true); | ||
| } | ||
|
|
||
| return result; | ||
| }); | ||
| } | ||
|
|
||
| async command(componentName, opts = {}, cb = function() {}) { | ||
| const { | ||
| hooksRetryTimeout = 10000, | ||
| hooksRetryInterval = 150, | ||
| playFnTimeout = 20000, | ||
| playFnRetryInterval = 100 | ||
| } = this.pluginSettings; | ||
|
|
||
| await this.api.launchComponentRenderer(); | ||
| await this.mountComponent(componentName, opts); | ||
|
|
||
| await this.api | ||
| .waitUntil(async () => { | ||
| if (this.client.argv.debug) { | ||
| return true; | ||
| } | ||
|
|
||
| const result = await this.api.execute(function() { | ||
| return !!window['@@component_class']; | ||
| }); | ||
|
|
||
| return !!result; | ||
| }, hooksRetryTimeout, hooksRetryInterval, this.getError(`time out reached (${hooksRetryTimeout}ms) while waiting for component to mount.`)) | ||
|
|
||
| // run the play() function | ||
| .execute(function(innerHTML) { | ||
| var scriptTag = Object.assign(document.createElement('script'), { | ||
| type: 'module', | ||
| innerHTML | ||
| }); | ||
| document.body.appendChild(scriptTag); | ||
| }, [` | ||
| const Component = window['@@component_class']; | ||
|
|
||
| if (Component && (typeof Component.play == 'function')) { | ||
| try { | ||
| window['@@playfn_result'] = await Component.play({ | ||
| component: window['@@component_element'] | ||
| }) || {}; | ||
| } catch (err) { | ||
| console.error('Error while executing .play() function:', err); | ||
| window.__$$PlayFnError = err; | ||
| } | ||
| } | ||
| window.__$$PlayFnDone = true; | ||
| `]); | ||
|
|
||
| if (this.client.argv.debug) { | ||
| await this.api.debug(); | ||
| } else if (this.client.argv.preview) { | ||
| await this.api.pause(); | ||
| } | ||
|
|
||
| const result = await this.api.execute(function() { | ||
| return document.querySelectorAll('#app')[0].firstElementChild; | ||
| }, []); | ||
|
|
||
| if (!result) { | ||
| const err = this.getError('Could not mount the component.'); | ||
|
|
||
| return err; | ||
| } | ||
|
|
||
| const componentInstance = this.api.createElement(result, { | ||
| isComponent: true, | ||
| type: 'svelte' | ||
| }); | ||
|
|
||
| cb(componentInstance); | ||
|
|
||
| return componentInstance; | ||
| } | ||
|
|
||
| static _getMockContent(mocks = {}) { | ||
| const definitions = Object.keys(mocks); | ||
|
|
||
| let mockContent = ''; | ||
| let mockFetch = false; | ||
| const mockFetchContent = ` | ||
| function mockApiResponse(body) { | ||
| return new window.Response(JSON.stringify(body), { | ||
| status: 200, | ||
| headers: { | ||
| 'Content-type': 'application/json' | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| const stubedFetch = sinon.stub(window, 'fetch'); | ||
| `; | ||
|
|
||
| let mockFetchItemsContent = ''; | ||
| if (definitions.length > 0) { | ||
| mockContent = 'import sinon from \'/node_modules/sinon/pkg/sinon-esm.js\';'; | ||
| mockFetchItemsContent = definitions.reduce((prev, mockUrl) => { | ||
| const {body, type = 'fetch'} = mocks[mockUrl]; | ||
| if (type === 'fetch') { | ||
| mockFetch = true; | ||
| } | ||
|
|
||
| prev += ` | ||
| stubedFetch.withArgs('${mockUrl}').returns(sinon.promise(function (resolve, reject) { | ||
| resolve(mockApiResponse(${JSON.stringify(body)})); | ||
| })); | ||
| `; | ||
|
|
||
| return prev; | ||
| }, ''); | ||
| } | ||
|
|
||
| if (mockFetch) { | ||
| mockContent += mockFetchContent; | ||
| mockContent += mockFetchItemsContent; | ||
| } | ||
|
|
||
| return mockContent; | ||
| } | ||
|
|
||
| static _buildScript(componentName, opts = {}) { | ||
|
|
||
| return ` | ||
| import Component from '${componentName}' | ||
|
|
||
| ${Command._getMockContent(opts.mocks)} | ||
|
|
||
| const element = new (Component || Component.default)({ | ||
| target: document.getElementById('app'), | ||
| props: ${JSON.stringify(opts.props || {})}, | ||
| context: ${opts.context}, | ||
| anchor: ${opts.anchor || null}, | ||
| intro: ${opts.intro || false} | ||
| }); | ||
|
|
||
| window['@@component_element'] = element; | ||
| window['@@component_class'] = Component; | ||
| window['@@playfn_result'] = null; | ||
| window.__$$PlayFnError = null; | ||
| window.__$$PlayFnDone = false; | ||
| `; | ||
| } | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.