Closed nothingismagick closed 4 years ago
As said on Discord channel, I'll write here updates needed to make Jest work together with Typescript. Many updates can be already done now by default even when TS isn't set, because they doesn't change anything for JS users.
{{ input }}
{{ counter }}babel-jest
(es. lodash-es
)I gave up after some hours of trial and error (mostly error and error actually).
I found a fixer which whitelists some particular modules by name back when I worked with Angular, but everything I tried now on Vue/Quasar world doesn't seem to work.
That workaround required to use babel.config.js
file, otherwise node_modules
could not be transformed.
It's not entirely clear to me which one is using now Quasar, because the project contains both a babel.config.js
and a .babelrc
(which is then imported into the first one) without comments about why is it so. An explanation on this point would be interesting.
For lodash-es
it's possible to stub it using moduleNameMapper
and map it to its non-ES6 counterpart, but it won't work with other libraries.
Reference
jest.config.js
moduleNameMapper: {
// ...
"^lodash-es$": "lodash",
}
Vue.use(Quasar, { components: { QBtn, ... } })
Into VSCode, using intellisense autocomplete for components doesn't automatically import those same components, but just autocompletes the corresponding object key. No idea how to solve this tho, using an array with a union type composed of all components instead of the current typed object would allow to insert multiple times the same component and would require a change current implementation.
It would be super-good to make factory
function to automatically infer props from the component (and I guess it's possible to do so via typeof
stuff or similar) but I could not find a way to do so.
const factory = (propsData: any /* should be automatically typed! */ = {}) => {
return shallowMount(QBtnDemo, {
localVue,
propsData,
});
};
Then it would be possible to create a reusable util function like the ones into Angular Spectator, taking as input localVue
and the component, and returning the factory.
Any help in proceeding further is appreciated.
@IlCallo one thing that we did when adding support for typescript in the main cli was to not include the extension when reference the files for import. If you use node module loading strategy it should import the file without a need for the extension to be specified, so setupFilesAfterEnv: ['<rootDir>/test/jest/jest.setup']
. I'm not 100% sure of the context because I haven't used the extension, but it may help make the code extension code more generic. Just a though as I read through your issue comments. Ignore this comment if it is completely out of context and not relevant at all....
Seems like removing the extension will work for JS-version, but will break when using a TS file.
That's probably because Jest actually runs on JS and therefore follows all JS conventions (so it searches a .js
file when the extension is not specified).
I'll look better into it after I fix linting stuff
Additional updates after typescript-eslint
v2 has been released
unbound-method false positive
Into app.spec.ts
it('has a created hook', () => {
// False positive
// See https://github.com/typescript-eslint/typescript-eslint/issues/692
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(typeof vm.increment).toBe('function');
});
Use native startWith instead of RegExp
Into test > cypress > support > index.js
use this version
const resizeObserverLoopError = 'ResizeObserver loop limit exceeded';
Cypress.on('uncaught:exception', err => {
if (err.message.startsWith(resizeObserverLoopError)) {
// returning false here prevents Cypress from
// failing the test
return false;
}
});
Use const
instead of let
Into lighthouse-runner.js
, Performance
is defined as let
but never re-assigned. Can be changed to const
Avoid async
for sync methods
jest-loader.js
returns an async
function, even if all operations are synchronous.
It should be removed.
I'm using quasar/typescript and followed this guide to make the project work.
Just a note here about importing ES6 modules in case it helps someone, and if someone can also help me understand what I did :laughing:
On my code I needed to import this quasar utils code:
import { testPattern } from 'quasar/src/utils/patterns';
and jest
as failing with
export const testPattern = {
^^^^^^
SyntaxError: Unexpected token 'export'
Strictly using the examples provided here wasn't helping me, so I updated jest.config.js
more or less alike the examples here, but changing the transform
rules.
I'm using babel-jest
for js
and html
. I guess it is correct since the project doesn't have any other html files besides the generated template. As .vue
files are transformed with vue-jest
and it already takes care of typescript afaik, I guess my configuration is correct on using ts-jest
only for .tsx?
files.
const esModules = ['quasar/lang', 'quasar/src/utils'].join('|');
module.exports = {
globals: {
__DEV__: true
},
...
transform: {
'^.+\\.(js|html)$': 'babel-jest',
'^.+\\.tsx?$': 'ts-jest',
'.*\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$':
'jest-transform-stub'
// use these if NPM is being flaky
// '.*\\.vue$': '<rootDir>/node_modules/@quasar/quasar-app-extension-testing-unit-jest/node_modules/vue-jest',
// '.*\\.js$': '<rootDir>/node_modules/@quasar/quasar-app-extension-testing-unit-jest/node_modules/babel-jest'
},
transformIgnorePatterns: [`<rootDir>/node_modules/(?!(${esModules}))`],
...
};
Now everything is working :+1: My vue smoking-code tests are still working as well so that's a good sign for me.
We will have to use dynamic dependencies here - which I don't like because that makes them "invisible" to real dep auditing.
Proposal: helper packages for extensions