Jest unit test in Angular app is throwing "Jest encountered an unexpected token" error

Discussão em 'Angular' iniciado por commadelimited, Janeiro 27, 2025.

  1. I'm receiving email messages from a local mail server and I need to be able to parse them in JavaScript. The @ProtonMail/jsmimeparser library is working flawlessly which is a huge relief. However, it's causing failures in my unit testing. It's not any specific test, but the harness for this particular page. Full error output is below.

    Versions:
    Angular 18.2.12
    Jest 29.7.0
    jsmimeparser 3.0.1


    Error output:

    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:

    /Users/amatthews/src/WEB/accounts-client/node_modules/@protontech/jsmimeparser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { parseMail } from './lib/mailParser';
    ^^^^^^
    SyntaxError: Unexpected token 'export'

    2 | import { Router, ActivatedRoute } from '@angular/router';
    3 | import { ReplaySubject, Subscription, combineLatest } from 'rxjs';
    > 4 | import { parseMail } from '@protontech/jsmimeparser';
    | ^
    5 | import { EmailMessage } from '../../model/email-message.model';
    6 | import { MailingListsService } from '../../service/mailing-lists.service';
    7 | import { Status } from 'src/app/model/status.model';

    at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
    at Object.<anonymous> (src/app/mailing-lists/component/message-queue/message-queue.component.ts:4:1)
    at Object.<anonymous> (src/app/mailing-lists/component/message-queue/message-queue.component.spec.ts:3:1)


    Sample message body

    {
    "request_id": "1",
    "original_size": 855,
    "sender": "dev.tester@tena-sda.org",
    "subject": "Testing one two three",
    "hold_date": new Date(),
    "msg": "Received: from [127.0.0.1] (localhost [127.0.0.1])\n\t(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)\n\t key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)\n\t(No client certificate requested)\n\t(Authenticated sender: dev.tester@tena-sda.org)\n\tby mailman.default.svc.cluster.local (Postfix) with ESMTPSA id A48A33E9849\n\tfor <abc-announce@tena-sda.org>; Thu, 23 Jan 2025 21:24:11 +0000 (UTC)\nMessage-ID: <3ecbaccf-f65a-4ce5-bdfa-e08b5659dc32@tena-sda.org>\nDate: Thu, 23 Jan 2025 15:24:11 -0600\nMIME-Version: 1.0\nUser-Agent: Mozilla Thunderbird\nContent-Language: en-US\nTo: abc-announce@tena-sda.org\nFrom: Dev Tester <dev.tester@tena-sda.org>\nSubject: Testing one two three\nContent-Type: text/plain; charset=UTF-8; format=flowed\nContent-Transfer-Encoding: 7bit\nX-MailFrom: dev.tester@tena-sda.org\nX-Mailman-Rule-Hits: nonmember-moderation\nX-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation\nMessage-ID-Hash: XM4OS47LU7DRYIO74WB5K4W345NPZ37O\nX-Message-ID-Hash: XM4OS47LU7DRYIO74WB5K4W345NPZ37O\n\nThis is a single line message body.\n\n"}


    Component

    ...
    ngOnInit() {
    this.mailingService.getHeldMessages('abc-announce').subscribe(
    response => {
    this.messages = (response as EmailMessage[]).map(
    message => {
    message.msg = this.formatMessage(message.msg);
    return message;
    }
    );
    },
    );
    }

    formatMessage(msg: string): string {
    // break message into the distinct parts of a MIME email.
    const email = parseMail(msg);
    return email.body.html;
    }
    ...


    Sample unit test

    describe('MessageQueueComponent', () => {
    let component: MessageQueueComponent;
    let fixture: ComponentFixture<MessageQueueComponent>;

    beforeEach(async () => {

    TestBed.configureTestingModule({
    imports: [],
    providers:[
    provideHttpClient(),
    provideHttpClientTesting(),
    ],
    })
    .compileComponents();

    fixture = TestBed.createComponent(MessageQueueComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    });

    it('should create', () => {
    expect(component).toBeTruthy();
    });
    });

    Continue reading...

Compartilhe esta Página