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

SyntaxError: Unexpected token 'export' #169

Open
adinanp opened this issue Apr 1, 2024 · 5 comments
Open

SyntaxError: Unexpected token 'export' #169

adinanp opened this issue Apr 1, 2024 · 5 comments

Comments

@adinanp
Copy link

adinanp commented Apr 1, 2024

When trying to recreate the examples from README, I get a type error.

Facatory:

import {
  CollectionSubfactory,
  Factory,
  LazyInstanceAttribute,
  FactorizedAttrs,
} from '@jorgebodega/typeorm-factory';
import { Document } from '../../../src/modules/documents/entities/document.entity';
import { faker } from '@faker-js/faker';
import { ContentType, Status } from '../../../src/modules/common/enums';
import { TagFactory } from '../commons/tags.factory';
import dataSource from '../../../src/modules/configuration/database/datasource';

export class DocumentFactory extends Factory<Document> {
  protected entity = Document;

  protected dataSource = dataSource;

  protected attrs(): FactorizedAttrs<Document> {
    return {
      title: faker.string.sample,
      thumbnail: faker.string.uuid,
      file: faker.string.uuid,
      slug: faker.string.sample,
      description: faker.string.sample,
      contentType: ContentType.ARTICLE,
      status: Status.ARCHIVED,
      tags: new LazyInstanceAttribute(
        () => new CollectionSubfactory(TagFactory, 1),
      ),
    };
  }
}

The error

SyntaxError: Unexpected token 'export'

Full stack trace (expand) ```
    ● 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/adinan.paiva/projects/sanar/learning-api/node_modules/@jorgebodega/typeorm-factory/dist/index.js:1
      ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './factory';
                                                                                        ^^^^^^
  
      SyntaxError: Unexpected token 'export'
  
      > 1 | import {
          | ^
        2 |   CollectionSubfactory,
        3 |   Factory,
        4 |   LazyInstanceAttribute,
  
        at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1796:14)
        at Object.<anonymous> (../test/factories/assets/document.factory.ts:1:1)
```

Versions:

├── typeorm@0.3.20
├── typescript@4.9.5
├── @jorgebodega/typeorm-factory@2.0.0
├── @nestjs/typeorm@9.0.1

This is a bug or a miss configuration?

Let me see if you need more reproducible information to help me.

Thanks

@giorgiofellipe
Copy link

Having the same. @adinanp did you find a work around?

@jorgebodega
Copy link
Owner

Can you provide a reproducible example? I will take a look in the following days.

But, I must say, this looks like a problem with node esm vs commonjs.

@giorgiofellipe
Copy link

I've fixed by adding these to my jest.config:

  globals: {
    'ts-jest': {
      tsconfig: {
        allowJs: true,
      },
    },
  },
  extensionsToTreatAsEsm: ['.ts'],
  transform: {
    '^.+\\.[tj]s$': 'ts-jest',
  },
  transformIgnorePatterns: ['/node_modules/(?!@jorgebodega/typeorm-factory)'],

@BenStirrup
Copy link

I confirm that the same error happened to me when upgrading to version 2.0.0

@tillkolter
Copy link

Same problem here. We are using mocha in a type: module package.

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

No branches or pull requests

5 participants