Skip to content

Commit

Permalink
test: add e2e case for conditional exports
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Jan 25, 2024
1 parent 8b5eed3 commit 8518798
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module.exports = {
{
files: ['**/e2e/**/*.cy.ts', '**/e2e/cypress/**/*.ts'],
extends: 'plugin:cypress/recommended',
rules: {
'@typescript-eslint/no-namespace': 'off',
},
},
{
files: ['**/tests/**/*.ts', 'tsup.config.ts'],
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ jobs:
env:
E2E_BASE: ${{ matrix.base }}
E2E_BUNDLER: ${{ matrix.bundler }}
E2E_COMMAND: dev

- name: E2E build
working-directory: ./e2e
run: pnpm e2e:ci:build
env:
E2E_BASE: ${{ matrix.base }}
E2E_BUNDLER: ${{ matrix.bundler }}
E2E_COMMAND: build

e2e-result:
if: ${{ always() }}
Expand Down
1 change: 1 addition & 0 deletions e2e/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export default defineConfig({
},
env: {
E2E_BASE: process.env.E2E_BASE ?? '/',
E2E_COMMAND: process.env.E2E_COMMAND ?? 'dev',
},
})
34 changes: 30 additions & 4 deletions e2e/cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
const URL_PREFIX = Cypress.env('E2E_BASE').replace(/\/$/, '')

// override the default cy.visit command to prepend base
// @ts-expect-error: could not type this method correctly
Cypress.Commands.overwrite('visit', (originalFn, url, options) =>
// @ts-expect-error: could not type this method correctly
originalFn(`${URL_PREFIX}${url}`, options),
Cypress.Commands.overwrite('visit', (originalFn, ...args) => {
// cy.visit(url, options)
if (typeof args[0] === 'string') {
// @ts-expect-error: could not type this correctly
return originalFn(`${URL_PREFIX}${args[0]}`, args[1])
}

// cy.visit(options)
return originalFn({
...args[0],
url: `${URL_PREFIX}${args[0].url}`,
})
})

// add a custom request command to prepend base
Cypress.Commands.add('requestWithBase', (url, options) =>
cy.request(`${URL_PREFIX}${url}`, options),
)

declare global {
namespace Cypress {
interface Chainable {
requestWithBase: <T = any>(
url: string,
options?: Partial<Cypress.RequestOptions>,
) => Chainable<Cypress.Response<T>>
}
}
}

export {}
5 changes: 5 additions & 0 deletions e2e/docs/imports/conditional-exports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script setup>
import str from '@vuepress-e2e/conditional-exports'
</script>

{{ str }}
1 change: 1 addition & 0 deletions e2e/modules/conditional-exports/browser.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'browser-mjs'
1 change: 1 addition & 0 deletions e2e/modules/conditional-exports/node.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'node-cjs'
1 change: 1 addition & 0 deletions e2e/modules/conditional-exports/node.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'node-mjs'
19 changes: 19 additions & 0 deletions e2e/modules/conditional-exports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@vuepress-e2e/conditional-exports",
"type": "module",
"exports": {
".": {
"types": "./types.d.ts",
"browser": "./browser.mjs",
"node": "./node.mjs",
"module": "./node.mjs",
"import": "./node.mjs",
"require": "./node.cjs",
"default": "./node.cjs"
},
"./*": "./*"
},
"main": "cjs.js",
"module": "esm.js",
"types": "types.d.ts"
}
2 changes: 2 additions & 0 deletions e2e/modules/conditional-exports/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const str: string
export default str
5 changes: 3 additions & 2 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
"private": true,
"type": "module",
"scripts": {
"e2e:build": "vuepress build docs --clean-cache --clean-temp",
"e2e:build": "E2E_COMMAND=build vuepress build docs --clean-cache --clean-temp",
"e2e:build-webpack": "E2E_BUNDLER=webpack pnpm e2e:build",
"e2e:ci:build": "pnpm e2e:build && start-server-and-test e2e:serve http-get://localhost:9080 e2e:run",
"e2e:ci:dev": "start-server-and-test e2e:dev http-get://127.0.0.1:9080 e2e:run",
"e2e:clean": "rimraf docs/.vuepress/.temp docs/.vuepress/.cache docs/.vuepress/dist",
"e2e:dev": "vuepress dev docs --clean-cache --clean-temp",
"e2e:dev": "E2E_COMMAND=dev vuepress dev docs --clean-cache --clean-temp",
"e2e:dev-webpack": "E2E_BUNDLER=webpack pnpm e2e:dev",
"e2e:open": "cypress open",
"e2e:run": "cypress run",
"e2e:serve": "anywhere -s -h localhost -p 9080 -d docs/.vuepress/dist"
},
"dependencies": {
"@vuepress-e2e/conditional-exports": "file:./modules/conditional-exports",
"@vuepress/bundler-vite": "workspace:*",
"@vuepress/bundler-webpack": "workspace:*",
"sass": "^1.70.0",
Expand Down
13 changes: 13 additions & 0 deletions e2e/tests/imports/conditional-exports.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
it('should support visiting non-ASCII paths directly', () => {
const E2E_COMMAND = Cypress.env('E2E_COMMAND')

cy.visit('/imports/conditional-exports.html')

cy.get('.e2e-theme-content p').should('have.text', 'browser-mjs')

if (E2E_COMMAND === 'build') {
cy.requestWithBase('/imports/conditional-exports.html')
.its('body')
.should('include', '<p>node-mjs</p>')
}
})
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8518798

Please sign in to comment.