-
Notifications
You must be signed in to change notification settings - Fork 62
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
Conversation
75811d8
to
bf447c3
Compare
@@ -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"; |
There was a problem hiding this comment.
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
src/bundle/test-transformer.ts
Outdated
|
||
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"]; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
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", { |
There was a problem hiding this comment.
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.
bf447c3
to
6023993
Compare
…rd party libraries
6023993
to
86bdb90
Compare
What is done
Some libraries can load css files when you import them in component test file. For example:
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.