Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(component-testing): correctly import components with css from third party libraries #1023

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

DudaGod
Copy link
Member

@DudaGod DudaGod commented Oct 7, 2024

What is done

Some libraries can load css files when you import them in component test file. For example:

// test.testplane.tsx
import {Button} from "some-component-library";

describe("suite", {...});

This file executes not only in browser, but also in nodejs environment. Where I get an error like: Unexpected token ... when try to import it. To solve this problem we use pirates + babel, they remove unnecessary imports. But when user import some module (without file extension) we can't figure out if it needs to be ignored in node env or not.

So in order to fix this problem we first require this module and if we get Unexpected token ... error from css file then we remove it.

@DudaGod DudaGod force-pushed the TESTPLANE-273.fix_import_css branch from 75811d8 to bf447c3 Compare October 8, 2024 09:55
@@ -2,10 +2,14 @@ import * as nodePath from "node:path";
import * as babel from "@babel/core";
import { addHook } from "pirates";
import { TRANSFORM_EXTENSIONS, JS_EXTENSION_RE } from "./constants";
import { requireModuleSync } from "../utils/module";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper in order to be able to test require


import type { NodePath, PluginObj, TransformOptions } from "@babel/core";
import type { ImportDeclaration } from "@babel/types";

const STYLE_EXTESTION_RE = /\.(css|less|scss)$/;
const IGNORE_IMPORT_ERRORS = ["Unexpected token"];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When style files imported in node js environment I get an error like: Unexpected token . (when first line in css is .some-class {...} or Unexpected token { (when first line is body {...})


const firstStackFrame = (err as Error).stack?.split("\n")[0] || "";
const filePath = firstStackFrame.split(":")[0];
const isStyleFilePath = STYLE_EXTESTION_RE.test(filePath);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I get an error from import css first frame in stack looks like: /Users/.../some.css:100500.

[".css", ".less", ".scss", ".jpg", ".png", ".woff"].forEach(extName => {
it(`asset with extension: "${extName}"`, () => {
describe("'removeNonJsImports' option", () => {
[true, false].forEach(removeNonJsImports => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just move to common describe

const error = { message: "Unexpected token {", stack: `foo${extName}:100500\nbar\nqux` };

const { setupTransformHook } = proxyquire("../../../src/test-reader/test-transformer", {
"../bundle": proxyquire.noCallThru().load("../../../src/bundle/test-transformer", {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I use two proxyquire in order to correctly check the logic. Didn't find another solution.

@DudaGod DudaGod force-pushed the TESTPLANE-273.fix_import_css branch from bf447c3 to 6023993 Compare October 8, 2024 10:04
@DudaGod DudaGod force-pushed the TESTPLANE-273.fix_import_css branch from 6023993 to 86bdb90 Compare October 24, 2024 09:30
@DudaGod DudaGod merged commit 591cd5a into master Oct 24, 2024
2 checks passed
@DudaGod DudaGod deleted the TESTPLANE-273.fix_import_css branch October 24, 2024 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants