aurelia / aurelia/framework

Test Problem when I have a two plugin inside of another.

Open
#1,012 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.7k
Forks
608
PR merge metrics
No merged PRs in 30d

Description

Here’s the revised bug report in English, incorporating the fact that the suggested Jest configuration did not resolve the issue:




🐛 Bug Report


Repro link:


🤔 Expected Behavior


When creating a custom plugin that depends on another custom plugin, the test suite should run without throwing parsing or configuration errors.


😯 Current Behavior


// Test Code  

import { HeaderContext } from './../../src/contexts/ig-header-context';
import { BindingSignaler } from 'aurelia-templating-resources';
import { NavigationInstruction } from 'aurelia-router';
import {ComponentTester, StageComponent} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrapper';
import { Container } from 'aurelia-framework';
const navigationInstructionMock: any = {
params: {},
queryParams: {},
config: {
name: 'testRoute'
}
};

describe('HeaderContext', () => {
let signaler: BindingSignaler;
let container: Container;
let component: ComponentTester;
let headerContext: HeaderContext;
beforeEach(() => {
signaler = new BindingSignaler();
container = new Container();
headerContext = new HeaderContext(signaler);
container.registerInstance(HeaderContext, HeaderContext);
component = StageComponent
.withResources();
component.bootstrap(aurelia => {
aurelia.use.standardConfiguration();
aurelia.container = container;
return aurelia.use;
});
});

it('should resolve and use all dependencies correctly', done => {
component.create(bootstrap).then(() => {
headerContext.setInstruction(navigationInstructionMock);
headerContext.setPageTitle()
expect(headerContext.setInstruction).toHaveBeenCalled();
done();
}).catch(done.fail);
});

it('should initialize with default values', () => {
component.create(bootstrap).then(() => {
expect(headerContext.id).toBe("");
expect(headerContext.grupo).toBe("");
expect(headerContext.sujeto).toBe("");
expect(headerContext.propiedades).toEqual({});
expect(headerContext.title).toBe("");
expect(headerContext.instruction).toBeNull();
expect(headerContext.affix).toBe(false);
expect(headerContext.closed).toBe(true);
});
});

it('should set instruction and update title', () => {
const instruction: NavigationInstruction = {
config: { navModel: { title: 'Test Title' } }
} as NavigationInstruction;
headerContext.setInstruction(instruction);
expect(headerContext.instruction).toBe(instruction);
expect(headerContext.title).toBe('Test Title');
});

it('should reset properties and signal changes', () => {
jest.spyOn(signaler, 'signal');
headerContext.grupo = "test";
headerContext.sujeto = "test";
headerContext.propiedades = { key: "value" };
headerContext.reset();
expect(headerContext.grupo).toBe("");
expect(headerContext.sujeto).toBe("");
expect(headerContext.propiedades).toEqual({});
expect(headerContext.closed).toBe(true);
expect(signaler.signal).toHaveBeenCalledWith("header-context-sujeto");
expect(signaler.signal).toHaveBeenCalledWith("header-context-properties");
});

it('should initialize with given data', () => {
const data = { id: "123", grupo: "test" };
headerContext.init(data);
expect(headerContext.id).toBe("123");
expect(headerContext.grupo).toBe("test");
expect(headerContext.closed).toBe(false);
});

it('should update sujeto and signal change', () => {
jest.spyOn(signaler, 'signal');
headerContext.actualizarSujeto("new sujeto");
expect(headerContext.sujeto).toBe("new sujeto");
expect(signaler.signal).toHaveBeenCalledWith("header-context-sujeto");
});

it('should update propiedades and signal change', () => {
jest.spyOn(signaler, 'signal');
const propiedades = { key1: "value1", key2: "value2" };
headerContext.actualizarPropiedades(propiedades);
expect(headerContext.propiedades).toEqual(propiedades);
expect(signaler.signal).toHaveBeenCalledWith("header-context-properties");
});

it('should truncate long propiedades values', () => {
const longValue = "a".repeat(51);
const propiedades = { key: longValue };
headerContext.actualizarPropiedades(propiedades);
expect(headerContext.propiedades.key).toBe(longValue.substring(0, 50) + "..");
});

it('should format date propiedades values', () => {
const dateValue = "2023-10-10T10:10:10Z";
const propiedades = { key: dateValue };
headerContext.actualizarPropiedades(propiedades);
expect(headerContext.propiedades.key).toBe("10/10/2023 10:10");
});
});


Running tests with Jest on a plugin that imports a class from another custom plugin results in the following error:


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.

Details:

/home/carloscarniatto/Projects/Aurelia/FrontEnd/ig-components/node_modules/.pnpm/@intrigest+core@5.2.1/node_modules/@intrigest/core/src/utils/utils-dates.ts:2
import { es } from "date-fns/locale";
^^^^^^
SyntaxError: Cannot use import statement outside a module


The error occurs in the following code:


import { UtilsDates } from '@intrigest/core/src/utils/utils-dates';  


💁 Possible Solution


I have already tried the following Jest configuration, but it did not resolve the issue:


module.exports = {  

transformIgnorePatterns: ["/node_modules/(?!@intrigest/core)"],
transform: {
"^.+\\.tsx?$": "ts-jest"
},
moduleNameMapper: {
"^.+\\.ts$": "ts-jest"
}
};

Other possible solutions to explore:


Add a more advannced Test Example

🔦 Context


This issue prevents the execution of tests, blocking development and validation of features that depend on custom plugins.


💻 Code Sample


// File that triggers the error  

import { UtilsDates } from '@intrigest/core/src/utils/utils-dates';
import { autoinject } from "aurelia-framework";

🌍 Your Environment

Software | Version(s)
-- | --
Aurelia Framework | 1.4.1
Language | TypeScript
Browser | N/A
Bundler | Webpack
Operating System | Linux
NPM/Node/Yarn | Node 18, npm 8.3.0



Let me know if you want to adjust anything or include additional details!

Contributor guide

Open the contributing guide

Research direction

Start with the failing import from @intrigest/core/src/utils/utils-dates and the Jest configuration shown in the issue. Run the affected Jest suite and trace how the TypeScript dependency in node_modules is transformed; done means tests involving nested custom plugins run without the unexpected-token parsing error.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript, webpack
Domain
testing, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.