Skip to content

Commit

Permalink
feat: document and prefer Vitest config test.setupFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
dnotes committed Oct 8, 2024
1 parent f1eb2b0 commit a02714f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
48 changes: 30 additions & 18 deletions packages/main/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,51 @@ npm install --save-dev quickpickle vitest

## Configuration

Add the quickpickle plugin to your Vitest configuration in vite.config.ts (or .js, etc.):
Add the quickpickle plugin to your Vitest configuration in vite.config.ts (or .js, etc.).
Also add the configuration to get the feature files, step definitions, world file, etc.

```ts
// vite.config.ts
import { quickpickle } from 'quickpickle';

const qpConfig:Partial<QuickPickleConfig> = { // <-- Optional configuration (defaults shown)

/**
* The files to be imported for each Feature.
* All step definitions, hooks, world constructor, etc. must be listed.
* The value can be a glob pattern or an array of glob patterns.
*/
import: [
'{features,test,tests}/**/*.steps.{ts,js,mjs}',
'{features,test,tests}/**/*.world.{ts,js,mjs}'
]

}

export default {
plugins: [
quickpickle(qpConfig) // <-- Add the quickpickle plugin
quickpickle() // <-- Add the quickpickle plugin
],
test: {
include : [
'features/*.feature', // <-- Add Gherkin feature files into "test" configuration
// (you'll probably want other test files too, for unit tests etc.)
],
setupFiles: ['./tests/tests.steps.ts'] // <-- specify each setupfile here
},
};
```

If you have multiple configurations, you can also add the test settings in vitest.workspace.ts.
This is also where you could set up separate configurations for components vs. application,
different browser environments, different world constructors, etc.

```ts
// vitest.workspace.ts
import { defineWorkspace } from 'vitest/config';

export default defineWorkspace([
{ // configuration for feature files
extends: './vite.config.ts',
test: {
include : [ 'tests/*.feature' ],
setupFiles: [ 'tests/tests.steps.ts' ],
},
},
{ // configuration for unit tests
test: {
include : [ 'tests/*.test.ts' ],
}
}
])
```

## Usage

### Write Feature files
Expand Down Expand Up @@ -100,11 +112,11 @@ npx vitest --run
### Write step definitions

Write your step definitions in a typescript or javascript file as configured
in the "import" declaration of the qpConfig object above.
in the "setupFiles" declaration in your vitest config.

These files will be imported into the Vitest test runner, and the code for
`Given`, `When`, `Then` will register each of the step definitions with quickpickle.
These step definitions should run on import, i.e. at the top level of the script,
These step definitions should run immediately, i.e. at the top level of the script,
not as exported functions like a normal node module would do.

```ts
Expand Down
5 changes: 3 additions & 2 deletions packages/main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export type QuickPickleConfig = {
const defaultConfig: QuickPickleConfig = {

/**
* @deprecated -- use the Vitest config test.setupFiles insetad
* The files to be imported for each Feature.
* All step definitions, hooks, world constructor, etc. must be listed.
* The value can be a glob pattern or an array of glob patterns.
Expand All @@ -98,7 +99,7 @@ const defaultConfig: QuickPickleConfig = {

interface ResolvedConfig {
test?: {
cucumber?: Partial<QuickPickleConfig>;
quickpickle?: Partial<QuickPickleConfig>;
};
}

Expand All @@ -110,7 +111,7 @@ export const quickpickle = function() {
configResolved: (resolvedConfig: ResolvedConfig) => {
config = defaults(
defaultConfig,
get(resolvedConfig, 'test.cucumber')
get(resolvedConfig, 'test.quickpickle')
) as QuickPickleConfig;
},
transform: async (src: string, id: string): Promise<string | undefined> => {
Expand Down
3 changes: 0 additions & 3 deletions packages/main/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { quickpickle } from './src';

export default {
plugins: [quickpickle()],
test: {
include : [ '{features,test,tests}/**/*.feature', 'tests/**/*.test.ts' ],
},
resolve: {
alias: {
'quickpickle': __dirname + '/src', // only needed because this is the quickpickle repository
Expand Down
16 changes: 16 additions & 0 deletions packages/main/vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineWorkspace, defineProject } from 'vitest/config';

export default defineWorkspace([
{
extends: './vite.config.ts',
test: {
include : [ 'tests/*.feature' ],
setupFiles: [ 'tests/tests.steps.ts' ],
},
},
{
test: {
include : [ './tests/*.test.ts' ],
}
}
])

0 comments on commit a02714f

Please sign in to comment.