Skip to content

Commit

Permalink
docs: debug Jest tests using Playwright (Chromium)
Browse files Browse the repository at this point in the history
Spawned from vitejs#7097
  • Loading branch information
MadLittleMods committed Mar 1, 2022
1 parent a49e198 commit 319f9de
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 109 deletions.
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ If you want to use break point and explore code execution you can use the ["Run

5. The execution will stop and you'll use the [Debug toolbar](https://code.visualstudio.com/docs/editor/debugging#_debug-actions) to continue, step over, restart the process...

### Debugging errors in Jest tests using Playwright (Chromium)

Some errors are masked and hidden away because of the layers of abstraction and sandboxed nature added by Jest, Playwright, and Chromium. In order to see what's actually going wrong and the contents of the devtools console in those instances, follow this setup:

1. Add a `debugger` statement to the `scripts/jestPerTestSetup.ts` -> `afterAll` hook. This will pause execution before the tests quit and the Playwright browser instance exits.

1. Run the tests with the `debug-serve` script command which will enable remote debugging: `pnpm run debug-serve -- --runInBand resolve`.

1. Wait for inspector devtools to open in your browser and the debugger to attach.

1. In the sources panel in the right column, click the play button to resume execution and allow the tests to run which will open a Chromium instance.

1. Focusing the Chomium instance, you can open the browser devtools and inspect the console there to find the underlying problems.

1. To close everything, just stop the test process back in your terminal.

## Testing Vite against external packages

You may wish to test your locally-modified copy of Vite against another package that is built with Vite. For pnpm, after building Vite, you can use [`pnpm.overrides`](https://pnpm.io/package_json#pnpmoverrides). Please note that `pnpm.overrides` must be specified in the root `package.json` and you must first list the package as a dependency in the root `package.json`:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"lint": "eslint packages/*/{src,types}/**",
"test": "run-s test-serve test-build",
"test-serve": "jest",
"debug-serve": "cross-env VITE_DEBUG_SERVE=1 node --inspect-brk ./node_modules/.bin/jest",
"debug-serve": "cross-env VITE_DEBUG_SERVE=1 node --inspect-brk ./node_modules/jest/bin/jest.js",
"test-build": "cross-env VITE_TEST_BUILD=1 jest",
"debug-build": "cross-env VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 node --inspect-brk ./node_modules/.bin/jest",
"docs": "vitepress dev docs",
Expand Down
210 changes: 105 additions & 105 deletions packages/playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
@@ -1,123 +1,123 @@
import { isBuild } from '../../testUtils'

test('bom import', async () => {
expect(await page.textContent('.utf8-bom')).toMatch('[success]')
})
// test('bom import', async () => {
// expect(await page.textContent('.utf8-bom')).toMatch('[success]')
// })

test('deep import', async () => {
expect(await page.textContent('.deep-import')).toMatch('[2,4]')
})
// test('deep import', async () => {
// expect(await page.textContent('.deep-import')).toMatch('[2,4]')
// })

test('entry with exports field', async () => {
expect(await page.textContent('.exports-entry')).toMatch('[success]')
})
// test('entry with exports field', async () => {
// expect(await page.textContent('.exports-entry')).toMatch('[success]')
// })

test('deep import with exports field', async () => {
expect(await page.textContent('.exports-deep')).toMatch('[success]')
})
// test('deep import with exports field', async () => {
// expect(await page.textContent('.exports-deep')).toMatch('[success]')
// })

test('deep import with query with exports field', async () => {
expect(await page.textContent('.exports-deep-query')).not.toMatch('fail')
})
// test('deep import with query with exports field', async () => {
// expect(await page.textContent('.exports-deep-query')).not.toMatch('fail')
// })

test('deep import SVG with query with exports field', async () => {
expect(await page.textContent('.exports-deep-svg-query')).not.toMatch('fail')
})

test('deep import HTML with query with exports field', async () => {
expect(await page.textContent('.exports-deep-html-query')).not.toMatch('fail')
})
// test('deep import HTML with query with exports field', async () => {
// expect(await page.textContent('.exports-deep-html-query')).not.toMatch('fail')
// })

test('deep import non-ESM with query with exports field', async () => {
expect(await page.textContent('.exports-deep-non-esm-query')).not.toMatch(
'fail'
)
})

test('deep import with exports field + exposed dir', async () => {
expect(await page.textContent('.exports-deep-exposed-dir')).toMatch(
'[success]'
)
})

test('deep import with exports field + mapped dir', async () => {
expect(await page.textContent('.exports-deep-mapped-dir')).toMatch(
'[success]'
)
})

test('Respect exports field env key priority', async () => {
expect(await page.textContent('.exports-env')).toMatch('[success]')
})

test('Respect production/development conditionals', async () => {
expect(await page.textContent('.exports-env')).toMatch(
isBuild ? `browser.prod.mjs` : `browser.mjs`
)
})

test('implicit dir/index.js', async () => {
expect(await page.textContent('.index')).toMatch('[success]')
})

test('implicit dir/index.js vs explicit file', async () => {
expect(await page.textContent('.dir-vs-file')).toMatch('[success]')
})

test('exact extension vs. duplicated (.js.js)', async () => {
expect(await page.textContent('.exact-extension')).toMatch('[success]')
})

test('dont add extension to directory name (./dir-with-ext.js/index.js)', async () => {
expect(await page.textContent('.dir-with-ext')).toMatch('[success]')
})

test('a ts module can import another ts module using its corresponding js file name', async () => {
expect(await page.textContent('.ts-extension')).toMatch('[success]')
})

test('filename with dot', async () => {
expect(await page.textContent('.dot')).toMatch('[success]')
})

test('browser field', async () => {
expect(await page.textContent('.browser')).toMatch('[success]')
})

test('css entry', async () => {
expect(await page.textContent('.css')).toMatch('[success]')
})

test('monorepo linked dep', async () => {
expect(await page.textContent('.monorepo')).toMatch('[success]')
})

test('plugin resolved virtual file', async () => {
expect(await page.textContent('.virtual')).toMatch('[success]')
})

test('plugin resolved custom virtual file', async () => {
expect(await page.textContent('.custom-virtual')).toMatch('[success]')
})

test('resolve inline package', async () => {
expect(await page.textContent('.inline-pkg')).toMatch('[success]')
})

test('resolve.extensions', async () => {
expect(await page.textContent('.custom-ext')).toMatch('[success]')
})

test('resolve.mainFields', async () => {
expect(await page.textContent('.custom-main-fields')).toMatch('[success]')
})

test('resolve.conditions', async () => {
expect(await page.textContent('.custom-condition')).toMatch('[success]')
})

test('resolve package that contains # in path', async () => {
expect(await page.textContent('.path-contains-sharp-symbol')).toMatch(
'[success]'
)
})
// test('deep import with exports field + exposed dir', async () => {
// expect(await page.textContent('.exports-deep-exposed-dir')).toMatch(
// '[success]'
// )
// })

// test('deep import with exports field + mapped dir', async () => {
// expect(await page.textContent('.exports-deep-mapped-dir')).toMatch(
// '[success]'
// )
// })

// test('Respect exports field env key priority', async () => {
// expect(await page.textContent('.exports-env')).toMatch('[success]')
// })

// test('Respect production/development conditionals', async () => {
// expect(await page.textContent('.exports-env')).toMatch(
// isBuild ? `browser.prod.mjs` : `browser.mjs`
// )
// })

// test('implicit dir/index.js', async () => {
// expect(await page.textContent('.index')).toMatch('[success]')
// })

// test('implicit dir/index.js vs explicit file', async () => {
// expect(await page.textContent('.dir-vs-file')).toMatch('[success]')
// })

// test('exact extension vs. duplicated (.js.js)', async () => {
// expect(await page.textContent('.exact-extension')).toMatch('[success]')
// })

// test('dont add extension to directory name (./dir-with-ext.js/index.js)', async () => {
// expect(await page.textContent('.dir-with-ext')).toMatch('[success]')
// })

// test('a ts module can import another ts module using its corresponding js file name', async () => {
// expect(await page.textContent('.ts-extension')).toMatch('[success]')
// })

// test('filename with dot', async () => {
// expect(await page.textContent('.dot')).toMatch('[success]')
// })

// test('browser field', async () => {
// expect(await page.textContent('.browser')).toMatch('[success]')
// })

// test('css entry', async () => {
// expect(await page.textContent('.css')).toMatch('[success]')
// })

// test('monorepo linked dep', async () => {
// expect(await page.textContent('.monorepo')).toMatch('[success]')
// })

// test('plugin resolved virtual file', async () => {
// expect(await page.textContent('.virtual')).toMatch('[success]')
// })

// test('plugin resolved custom virtual file', async () => {
// expect(await page.textContent('.custom-virtual')).toMatch('[success]')
// })

// test('resolve inline package', async () => {
// expect(await page.textContent('.inline-pkg')).toMatch('[success]')
// })

// test('resolve.extensions', async () => {
// expect(await page.textContent('.custom-ext')).toMatch('[success]')
// })

// test('resolve.mainFields', async () => {
// expect(await page.textContent('.custom-main-fields')).toMatch('[success]')
// })

// test('resolve.conditions', async () => {
// expect(await page.textContent('.custom-condition')).toMatch('[success]')
// })

// test('resolve package that contains # in path', async () => {
// expect(await page.textContent('.path-contains-sharp-symbol')).toMatch(
// '[success]'
// )
// })
10 changes: 7 additions & 3 deletions packages/playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ <h2>resolve package that contains # in path</h2>
import deepSvgPath from 'resolve-exports-path/deep.svg?url'
text('.exports-deep-svg-query', deepSvgPath)

// deep import HTML w/ exports w/ query
import deepHtmlPath from 'resolve-exports-path/deep.html?url'
text('.exports-deep-html-query', deepHtmlPath)
// console.log('before import')
// // deep import HTML w/ exports w/ query
// import deepHtmlPath from 'resolve-exports-path/deep.html?url'
// console.log('deepHtmlPath', deepHtmlPath)
// text('.exports-deep-html-query', deepHtmlPath)
// console.log('after import')
// debugger;

// deep import non-ESM w/ exports w/ query
import deepNonEsmPath from 'resolve-exports-path/deep-non-esm.js?url'
Expand Down
1 change: 1 addition & 0 deletions packages/vite/bin/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if (debugIndex > 0) {
}

function start() {
console.log('local vite awfeeffaew')
require('../dist/node/cli')
}

Expand Down
1 change: 1 addition & 0 deletions scripts/jestPerTestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ beforeAll(async () => {
}, 30000)

afterAll(async () => {
debugger
global.page?.off('console', onConsole)
global.serverLogs = []
await global.page?.close()
Expand Down

0 comments on commit 319f9de

Please sign in to comment.