Comments (5)
I am running into errors related to modules primarily because I think jest uses the CJS module system whereas the rest of the sa11y packages are using the ES modules. Not sure if it would help if I split the integration tests from the mono-repo into its own project.
In the integration tests package:
Baseline
- Using the
toBeAccessible()
API after explicitly invoking theregisterA11yMatchers()
in the test module works as illustrated in this issue above.
extending jest.config.js
directly with setup from sa11y
- When I create a
jest.config.js
with
const { jestConfig } = require('@sa11y/jest');
module.exports = {
...jestConfig,
}
I run into the following error: SyntaxError: Unexpected token 'export'
$ /Users/mrajamanickam/src/sa11y/foundation/node_modules/.bin/jest --no-cache
/Users/mrajamanickam/src/sa11y/foundation/packages/jest/dist/index.js:8
export { toBeAccessible } from './matcher';
^^^^^^
SyntaxError: Unexpected token 'export'
The error means that jest is making use of the extended config, but is not able to understand the ES module syntax.
extending with local setup file
- When I use
setupFilesAfterEnv
injest.config.js
instead
module.exports = {
setupFilesAfterEnv: ['./jestSetup.js'],
}
with a local file jestSetup.js
:
const { registerA11yMatchers } = require('@sa11y/jest');
registerA11yMatchers();
I get no errors when running jest - but the integration test fails because the toBeAccessible
is not defined which probably means that the setup is not working. Also tried with
setupFilesAfterEnv: [require('./jestSetup.js')],
setupFilesAfterEnv: [require('<rootDir>/jestSetup.js')],
● integration test @sa11y/jest › should have a11y matchers working with setup in jest.config.js
expect(received).toBeDefined()
Received: undefined
12 | // registerA11yMatchers();
13 | it('should have a11y matchers working with setup in jest.config.js', () => {
> 14 | expect(expect.toBeAccessible).toBeDefined();
| ^
15 | // TODO(Fix) : Fails with TypeError: expect(...).toBeAccessible is not a function
16 | expect(document).toBeAccessible();
17 | });
Tried out many suggestions from
- Unexpected Token Import for ES6 modules · Issue #2081 · facebook/jest · GitHub
- Dynamic import not working if using .babelrc · Issue #2442 · facebook/jest · GitHub
- babel-jest does not transpile import/export in node_modules when Babel 7 is used · Issue #6229 · facebook/jest · GitHub
including
- adding babel plugins to
babel.config.js
- didn't help
env: {
test: {
plugins: ['transform-es2015-modules-commonjs', 'dynamic-import-node', 'babel-plugin-transform-dynamic-import'],
},
},
- adding
"type": "module"
to the @sa11y/jest package.json- got
Error: Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension
- got
@trevor-bliss If you have any suggestions please let me know.
from sa11y.
Ok I pulled your branch and tried it out. I got it working after making a couple changes.
- Update dist files to be commonjs format
You were on to something when you mentioned the module format. You don't have to update any of your authored source code, but in the root level tsconfig.common.js
file, you can change the module
entry to commonjs
so the dist files you produce are in a more compatible format.
- Fix
packages/jest/package.json
The main
and types
entries are wrong here. Should point to dist/index.js
instead of dist/jest.js
.
- Simplify the Jest setup
This one is broader and more my opinion than a "bug". I think having @sally/jest
export a jest config object is doing too much. Instead, it should export the registerA11yMatchers
function and the consuming projects can call that function themselves in their Jest setup. sfdx-lwc-jest
provides the full config because it's a full test runner. This is more of a "matchers" library being integrated into an existing Jest setup.
So what I would do is remove the registerA11yMatchers()
invocation in setup.ts
and update test-integ/jest.config.js
to:
module.exports = {
setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],
};
where jest-setup.js
is:
const { registerA11yMatchers } = require('@sa11y/jest');
registerA11yMatchers();
I think this is a reasonable thing for consuming projects to do so appropriate for this integration test package.
Finally, if you run the Jest tests from the top level this will still fail because that will use the root level jest config instead of the integration test package config. I'd either create a separate npm script for running tests in this package, or have each sub package extend a base config and then have this package also have the setupFilesAfterEnv
entry to register the matchers.
from sa11y.
That is awesome
- Updating the common tsconfig to commonjs fixed the issue
🥳 - I had fixed the incorrect
main
entry inpackage.json
in 85609d3 but forgot to push🤦 . Sorry about that. - I get what you are saying and guess am ok with it for now. Cleaned up code as per your suggestions. But sometime in the future would like to offer a simplified 1 step setup process rather than the 2 step process if thats something most consumers of the API would want and could benefit from. But if most of them already have a jest setup and jest config and have no issues with the 2 step process, we could leave it as is.
- Got the integrations tests with a different jest config to run from project root with the help of some config in the root jest config.
Thanks a lot for this!
from sa11y.
Fixed by #9
from sa11y.
If you came here because you had this error, be aware that the Api has changed from registerA11yMatchers
to setup
while initializing these custom matchers
https://www.npmjs.com/package/@sa11y/jest
const { setup } = require('@sa11y/jest'); // CommonJS
import { setup } from '@sa11y/jest'; // ES6
// Register the sa11y matcher
setup();
from sa11y.
Related Issues (20)
- Dependency Dashboard
- doc: create a Pull request template
- Missing deliverable code for @sa11y/[email protected] HOT 2
- build: remove semantic-release dependency
- doc(jest, wdio): add screenshots, GIF demos to jest, wdio packages HOT 1
- build: replace ncu with yarn upgrade HOT 1
- test(lint): upgrade to typescript-eslint v3.x, typescript v4.x and fix newly identified issues HOT 1
- Install fails on node version 18? HOT 2
- Feature request: add support for node@18 (current LTS) HOT 4
- build: improve versioning workflow HOT 2
- test(wdio): migrate from ts-node for wdio tests HOT 1
- feat(wdio): add screenshot, URL a11y issues output from @sa11y/wdio
- import { assertAccessible, assertAccessibleSync } problem HOT 9
- chore: find a better way to track TODOs in code HOT 1
- fix(jest): codecov, automatic check tests to work with Jest v27 HOT 4
- fix(jest): automatic checks should not be triggered for failed tests
- Button with shadow DOM contents reports "Buttons must have discernible text" HOT 7
- fix: errors with node lts v16 HOT 6
- fix: missing pkg dependency of @sa11y/common
- accesskeys rule not failing as expected HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sa11y.