Skip to content

Make Jest preset work on Windows #33

Description

@Mythra

Package version

2.0.28

Jest version

29.7.0

node-versions

20.9.0

npm-versions

10.2.3

Description

Hi 👋 This is my first frontend project in quite awhile, I was able to get the UI & styling working in the way I wanted. However, I'm now running into an error upon trying to actually write tests for these react components. Specifically jest gets an unexpected token even though I have setup the project according to the documentation as far as I can tell.

The exact error I'm getting is:

 FAIL  source/ui/components/account-bar/__tests__/account-bar.tsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.       
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    D:\github\<project-name>\packages\browser-extension\node_modules\@cloudscape-design\components\index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as Alert } from './alert';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      2 | import "./styles.css";
      3 |
    > 4 | import { TextContent } from "@cloudscape-design/components";
        | ^
      5 |
      6 | export default () => {
      7 |       return (

Source code

Although I can't share links directly to the code I can share the following snippets, please let me know if you need anymore:

pacakge.json
{
	"name": "<name>",
	"private": true,
	"license": "UNLICENSED",
	"version": "0.1.0",
	"description": "<description>",
	"author": "<authors>",
	"keywords": [
		"aws",
		"<censored>",
		"aws accounts",
		"aws console",
		"aws credentials"
	],
	"scripts": {
		"build": "cross-env TARGET=chromium,firefox webpack --config webpack/webpack.config.js",
		"format": "prettier . --write",
		"format:check": "prettier . --check",
		"package": "cross-env NODE_ENV=production TARGET=chromium,firefox PACKAGE_CRXS=true PACKAGE_XPIS=true webpack --config webpack/webpack.config.js",
		"package:chrome": "cross-env NODE_ENV=production TARGET=chromium PACKAGE_CRXS=true webpack --config webpack/webpack.config.js",
		"package:firefox": "cross-env NODE_ENV=production TARGET=firefox PACKAGE_CRXS=false PACKAGE_XPIS=true webpack --config webpack/webpack.config.js",
		"test": "jest --silent"
	},
	"dependencies": {
		"@cloudscape-design/collection-hooks": "^1.0.36",
		"@cloudscape-design/components": "^3.0.529",
		"@cloudscape-design/design-tokens": "^3.0.34",
		"@cloudscape-design/global-styles": "^1.0.23",
		"react": "^18.2.0",
		"react-dom": "^18.2.0",
		"react-virtual": "^2.10.4"
	},
	"devDependencies": {
		"@cloudscape-design/components-themeable": "^3.0.544",
		"@cloudscape-design/jest-preset": "^2.0.28",
		"@cloudscape-design/theming-build": "^1.0.51",
		"@types/chrome": "^0.0.260",
		"@types/firefox-webext-browser": "^120.0.0",
		"@types/jest": "^29.5.12",
		"@types/react": "^18.2.55",
		"@types/react-dom": "^18.2.19",
		"@types/react-test-renderer": "^18.0.7",
		"archiver": "^6.0.1",
		"copy-webpack-plugin": "^12.0.2",
		"cross-env": "^7.0.3",
		"crx": "^5.0.1",
		"css-loader": "^6.10.0",
		"git-revision-webpack-plugin": "^5.0.0",
		"jest": "^29.7.0",
		"merge": "^2.1.1",
		"mkdirp": "^3.0.1",
		"prettier": "3.2.5",
		"react-test-renderer": "^18.2.0",
		"sass": "^1.70.0",
		"sass-loader": "^14.1.0",
		"style-loader": "^3.3.4",
		"ts-jest": "^29.1.2",
		"ts-loader": "^9.5.1",
		"typescript": "^5.3.3",
		"webpack": "^5.90.1",
		"webpack-cli": "^5.1.4"
	},
	"overrides": {
		"react-virtual": {
			"react": "$react"
		}
	}
}
jest.config.js
const merge = require("merge");
const tsPreset = require("ts-jest/jest-preset");
const cloudscapePreset = require("@cloudscape-design/jest-preset");

module.exports = merge.recursive(tsPreset, cloudscapePreset, {
	globals: {
		GIT_SHORTHASH: "0000000",
		GIT_HASH: "0000000000000000000000000000000000000000",
	},
	moduleNameMapper: {
                // As an explanation for this -- we have some "developer custom" components that are always rendered in night theme, these are generated as recommended in the render documentation -- but we're not using them for this test case, so we know it's not these that are an issue.
                // There may be a problem in the future but i'm not worried about that right now.
		"@<redacted>//forced-night-theme/@cloudscape-design/components":
			"<rootDir>/build/cloudscape-devbar-theme/components",
		"@<redacted>//forced-night-theme/@cloudscape-design/design-tokens":
			"<rootDir>/build/cloudscape-devbar-theme/design-tokens",
		"^.+\\.(css|scss|sass)$": "<rootDir>/webpack/css-stub.js",
	},
	roots: ["<rootDir>/source"],
	testMatch: ["**/__tests__/**/*.+(ts|tsx|js)", "**/?(*.)+(spec|test).+(ts|tsx|js)"],
});
account-bar\index.tsx
import React from "react";
import "./styles.css";

import { TextContent } from "@cloudscape-design/components";

export default () => {
	return (
		<nav id="account-bar" aria-label="Account Bar">
			<TextContent>Hello this is a test!</TextContent>
		</nav>
	);
};
account-bar\__tests__\account-bar.tsx
import React from "react";
import ReactTestRenderer from "react-test-renderer";
import AccountBar from "../";

it("can render the accountbar", () => {
  const renderer = ReactTestRenderer.create(<AccountBar />);
  console.log(renderer.toJSON());
});

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions