1
0
Fork
You've already forked MyFin
1

Set up testing with jest #2

Merged
arran merged 5 commits from phloose/MyFin:testing into main 2021年03月25日 15:55:14 +01:00
Collaborator
Copy link

jest normally looks for a __tests__ folder in the root of the repository. Snapshots will be placed inside a __snapshots__ folder. I'd also suggest to keep the same folder structure in the tests folder as in the src folder.

Setup code can be placed in the file jest.setup.ts which will be loaded for each test file (reference).

* Adds [jest](https://jestjs.io/) framework with transformers for: * svelte components ([svelte-jester](https://github.com/mihar-22/svelte-jester)) * typescript modules ([ts-jest](https://github.com/kulshekhar/ts-jest)) * Adds [@testing-library/svelte](https://testing-library.com/docs/svelte-testing-library/intro) which provides utility functions for testing svelte components * Adds [@testing-library/jest-dom](https://testing-library.com/docs/ecosystem-jest-dom/) for extending jest matchers with handy custom DOM matchers * Adds [pouchdb-adapter-memory](https://www.npmjs.com/package/pouchdb-adapter-memory) which should be used in tests (see [Adapters section](https://rxdb.info/adapters.html) in RxDB docs) * Adds a snapshot resolver that will place jest [snapshots](https://jestjs.io/docs/snapshot-testing) in the top level folder and also keeps the same structure as the tests folder. jest normally looks for a `__tests__` folder in the root of the repository. Snapshots will be placed inside a `__snapshots__` folder. I'd also suggest to keep the same folder structure in the tests folder as in the src folder. Setup code can be placed in the file `jest.setup.ts` which will be loaded for each test file ([reference](https://jestjs.io/docs/configuration#setupfilesafterenv-array)).
Some components wrongly imported 'stores.js' instead of 'stores' which
is a typescript module and that way could not be resolved by ts-jest.
Owner
Copy link

Looks really clean, I appreciate you adding pouchdb-adapter-memory for future too.

I not sure I understand the __tests__ folder structure, will this directory exist beside each components or is there a single directory where all tests will belong?

Looks really clean, I appreciate you adding `pouchdb-adapter-memory` for future too. I not sure I understand the `__tests__` folder structure, will this directory exist beside each components or is there a single directory where all tests will belong?
Author
Collaborator
Copy link

__tests__ will be at located at the root of the repository and therefore be separate from the components and ts-modules in the src folder. I personally prefer to have a separate test folder. What do you think?

`__tests__` will be at located at the root of the repository and therefore be separate from the components and ts-modules in the `src` folder. I personally prefer to have a separate test folder. What do you think?
Author
Collaborator
Copy link

I would like to set up und use the pouchdb-adapter-memory only when tests are being run like described above. But i am having trouble to set up detecting if we are in a test environment or a production environment.

Normally you would do this via globals/environment variables process.env.NODE_ENV or import.meta.env.MODE (MDN ref). process.env.NODE_ENV is obviously only available in a Node environment in which the tests are being run. Snowpack does set import.meta.env.MODE to either "production" when building or "development" when running the dev server. Jest sets process.env.NODE_ENV to "test".

The problem with that is that jest or ts-jest does not understand import.meta or in other words does not fully support ES6 modules (only in jest@next with this setting and ts-jest@next) and the browser cannot handle process.env.NODE_ENV.

The simplest solution for this akward javascript ecosystem problem would be to only use the pouchdb-adapter-memory ...

I would like to set up und use the `pouchdb-adapter-memory` only when tests are being run like described above. But i am having trouble to set up detecting if we are in a test environment or a production environment. Normally you would do this via globals/environment variables `process.env.NODE_ENV` or `import.meta.env.MODE` ([MDN ref](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import.meta)). `process.env.NODE_ENV` is obviously only available in a Node environment in which the tests are being run. Snowpack does set `import.meta.env.MODE` to either "production" when building or "development" when running the dev server. Jest sets `process.env.NODE_ENV` to "test". The problem with that is that jest or ts-jest does not understand `import.meta` or in other words does not fully support ES6 modules (only in [jest@next](https://jestjs.io/docs/next/ecmascript-modules) with [this setting](https://jestjs.io/docs/next/configuration#extensionstotreatasesm-arraystring) and [ts-jest@next](https://kulshekhar.github.io/ts-jest/docs/next/guides/esm-support/)) and the browser cannot handle `process.env.NODE_ENV`. The simplest solution for this akward javascript ecosystem problem would be to only use the `pouchdb-adapter-memory` ...
Owner
Copy link

__tests__ will be at located at the root of the repository and therefore be separate from the components and ts-modules in the src folder. I personally prefer to have a separate test folder. What do you think?

Sounds good to me, keep the ./src directory structure clutter-free.

... pouchdb-adapter-memory ...

I see, maybe this is something that can be improved in the future - I don't see an issue with including the dep now.

> `__tests__` will be at located at the root of the repository and therefore be separate from the components and ts-modules in the `src` folder. I personally prefer to have a separate test folder. What do you think? Sounds good to me, keep the `./src` directory structure clutter-free. > ... pouchdb-adapter-memory ... I see, maybe this is something that can be improved in the future - I don't see an issue with including the dep now.
Sign in to join this conversation.
No reviewers
Labels
Clear labels
No items
No labels
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
arran/MyFin!2
Reference in a new issue
arran/MyFin
No description provided.
Delete branch "phloose/MyFin:testing"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?