Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Issues
  • #5308
Closed
Open
Issue created Oct 05, 2018 by Administrator@rootContributor

CRA 2 dynamic JSON import has different behavior in test and browser

Created by: samuelsantia

Is this a bug report?

Yes, I think so

Did you try recovering your dependencies?

Yes, I tested in a fresh project

Which terms did you search for in User Guide?

Running tests, search for json

Environment

System: OS: macOS High Sierra 10.13.6 CPU: x64 Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz Binaries: Node: 10.7.0 - /usr/local/bin/node Yarn: 1.10.1 - /usr/local/bin/yarn npm: 6.1.0 - /usr/local/bin/npm Browsers: Chrome: 69.0.3497.100 Firefox: 62.0 Safari: 12.0 npmPackages: react: ^16.5.2 => 16.5.2 react-dom: ^16.5.2 => 16.5.2 react-scripts: 2.0.4 => 2.0.4 npmGlobalPackages: create-react-app: Not Found

Steps to Reproduce

(Write your steps here:)

  1. yarn create create-react-app test
  2. cd test
  3. Create data.json and loader utility to dynamic import it
// data.json
[
  { "id": "test1", "message": "hello" },
  { "id": "test2", "message": "world" }
]
// loader.js
export default async function loader() {
  const data = await import('./data.json')
  console.log(data)
  return data
}
// index.js
import loader from './loader'

loader()
  1. Create a test for loader
// loader.spec.js
import loader from './loader'

describe('loader', () => {
  it('should import json in default key', async () => {
    const actual = await loader()

    expect(actual).toHaveProperty('default')
  })
})
  1. Run yarn start
  2. Run yarn test

Expected Behavior

The dynamic imported json is the same

Actual Behavior

the console log in browser outputs the esm module with default property

Module {default: Array(2), __esModule: true, Symbol(Symbol.toStringTag): "Module"}

captura de pantalla 2018-10-05 a las 11 32 00

the console log in test outputs the plain js converted array

[ { id: 'test1', message: 'hello' },
{ id: 'test2', message: 'world' } ]

captura de pantalla 2018-10-05 a las 11 32 13

Reproducible Demo

https://github.com/samuelsantia/create-react-app-dynamic-json-issue

I have tried in codesandbox but it not loads the json as module

Thanks for all :)

Assignee
Assign to
Time tracking