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

TypeError: require(...) is not a function #6046

Closed
devmetal opened this issue Apr 21, 2018 · 3 comments
Closed

TypeError: require(...) is not a function #6046

devmetal opened this issue Apr 21, 2018 · 3 comments

Comments

@devmetal
Copy link

TypeError: require(...) is not a function

What is the current behavior?

I try to create a simple wrapper around chokidar. Also i like to add some smoke tests with jest. I use flow types, and babel6 with flow and env presets.

This is my test code

/* eslint-env jest */

import fs from 'fs'
import path from 'path'
import watch from '../watch'

const testDir = path.join(__dirname, 'temp')
const testFile = path.join(testDir, 'test.txt')

describe('watch files in directory', () => {
  let watcher

  const onAdd = jest.fn()
  const onChange = jest.fn()
  const onUnlink = jest.fn()

  beforeAll(() => {
    fs.mkdirSync(testDir)

    watcher = watch(testDir)
    watcher.on('add', onAdd)
    watcher.on('change', onChange)
    watcher.on('unlink', onUnlink)
  })

  afterAll(() => {
    fs.rmdirSync(testDir)
  })

  describe('watch add event', () => {
    beforeAll(() => {
      fs.writeFileSync(testFile, 'Content')
    })

    it('on(\'add\') triggered', () => {
      expect(onAdd).toHaveBeenCalled()
    })
  })

  describe('watch change event', () => {
    beforeAll(() => {
      fs.appendFileSync(testFile, 'Changes')
    })

    it('on(\'change\') triggered', () => {
      expect(onChange).toHaveBeenCalled()
    })
  })

  describe('watch unlink event', () => {
    beforeAll(() => {
      fs.unlinkSync(testFile)
    })

    it('on(\'unlink\') triggered', () => {
      expect(onUnlink).toHaveBeenCalled()
    })
  })
})

This is the api:

// @flow

import chokidar from 'chokidar'

export type evt = 'add' | 'change' | 'unlink'

export interface WatchEvent {
  on(e: evt, handler: (path: string) => any): any;
}

export default (root: string): WatchEvent => {
  return chokidar.watch(root, {
    persistent: true,
    ignoreInitial: true
  })
}

Very simple, but with jest is can't start. When i start this with jest latest version, i get back this error:

/Users/uname/works/node/mylive/node_modules/readdirp/readdirp.js:55
    var api          =  require('./stream-api')();
                                               ^

TypeError: require(...) is not a function
    at readdir (/Users/uname/works/node/mylive/node_modules/readdirp/readdirp.js:55:48)
    at FSWatcher.<anonymous> (/Users/uname/works/node/mylive/node_modules/chokidar/lib/fsevents-handler.js:350:7)
    at /Users/uname/works/node/mylive/node_modules/graceful-fs/polyfills.js:287:18
    at FSReqWrap.oncomplete (fs.js:153:5)

But when i just start with babel build and simple node command, the api working as expected.

Please provide your exact Jest configuration

Not use any, just the .babelrc with env and flow presets.

Run npx envinfo --preset jest in your project directory and paste the
results here

npx: installed 1 in 4.267s

System:
OS: macOS High Sierra 10.13.4
CPU: x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
Binaries:
Node: 8.11.1 - ~/.nvm/versions/node/v8.11.1/bin/node
Yarn: 1.5.1 - /usr/local/bin/yarn
npm: 5.6.0 - ~/.nvm/versions/node/v8.11.1/bin/npm
npmPackages:
jest: ^22.4.3 => 22.4.3

@SimenB
Copy link
Member

SimenB commented Apr 22, 2018

The error is basically what #5888 tries to highlight, but due to the asynchronicity at play, it fails at a different place.

In your case the runtime stops here: https://github.com/facebook/jest/blob/4f64c4cfa951cc4c437a40650cec8d9b74735af8/packages/jest-runtime/src/index.js#L504-L507

The watchers are triggered asynchronously, and your synchronous code doesn't wait for it.

You can use something like https://www.npmjs.com/package/wait-for-expect to wait for the expect to pass, or just add a small timeout yourself

@SimenB SimenB closed this as completed Apr 22, 2018
@SimenB
Copy link
Member

SimenB commented Apr 22, 2018

We should really handle "someone is trying to use the jest-environment after it was torn down" better, though

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants