kentcdodds / babel-plugin-macros

🎣 Allows you to build simple compile-time libraries
https://npm.im/babel-plugin-macros
MIT License
2.62k stars 135 forks source link

Add isMacrosName option #133

Closed futagoza closed 4 years ago

futagoza commented 4 years ago

What: Resolving #131 by adding a new option called isMacrosName for the plugin.

Why: Allows the user to selectively accept modules as a macro for this plugin.

How: This option will optionally overwrite the original behaviour, so backwards compatibility is kept.

futagoza commented 4 years ago

@kentcdodds I tried adding tests, and they were passing, but for some reason, the test macros › supports macros from node_modules broke. Tried removing the tests (including the new fixture I added) and tried to run the tests again to check if that fixed it, but the runner refuses to run because there have been no changes since the last commit, what should I do?

EDIT: Just for reference, here are the tests and fixture I tried adding:

// Tests, added to: src/__tests__/index.js, after Line 412

    {
      title: 'when a custom isMacrosName option is used on a import',
      pluginOptions: {
        isMacrosName(v) {
          return v.endsWith('-macro.js')
        },
      },
      code: `
        import myEval from './fixtures/eval-macro.js'
        const x = myEval\`34 + 45\`
      `,
    },
    {
      title: 'when a custom isMacrosName option is used on a require',
      pluginOptions: {
        isMacrosName(v) {
          return v.endsWith('-macro.js')
        },
      },
      code: `
        const evaler = require('./fixtures/eval-macro.js')
        const x = evaler\`34 + 45\`
      `,
    },

// Fixture: src/__tests__/fixtures/eval-macro.js

    module.exports = require('./eval.macro.js')
kentcdodds commented 4 years ago

Thanks!

The terminal is interactive with Jest watch mode. Read the output and press the keys that make sense :)

futagoza commented 4 years ago

Without the new tests and fixture:

  1. I run npm run test
  2. It refuses to run because there have been no changes, and gives me options
  3. I press a (which it says runs all tests)
  4. It refuses to run because there have been no changes, and tells me to press w to see options
  5. I press w, and it shows the options again but a has disappeared, so I try every other option
  6. I see 4 again.... and again

When I add the new tests and fixture it passes these tests no problem, but macros › supports macros from node_modules breaks (an error says it expected the function to be called once, but it wasn't), but because I can't run the tests without new uncommitted tests, I can't tell if the problem is:

Is there any other way to force Jest to run the tests via kcd-scripts? While writing this it just occurred to me that I could try to create a new config and run the tests using npx jest --config new-config.js (don't know why I didn't think of it before when I was working on the fork a few hours ago, oh well). Looked at your jest config, but it looks like it's geared towards watch mode. Any tips that I can use when I get around to trying this later?

kentcdodds commented 4 years ago

That's odd. It should definitely run all the tests when you hit a.

You can force it to run the tests with npm run test -- --coverage and that'll run them all once and give you a coverage report.

Sorry about the trouble.

futagoza commented 4 years ago

lol A bit late for that, I created a new jest.config.js (same as the config from kcd-scripts but without all the watch mode options and normalized, see below) and ran npx jest and that confirmed everything was working, so it's an issue with Jest's watch mode (and probably a subtle windows only bug). To commit it though, I had to change the script test in package.json to an empty string before the husky plugin allowed it through 😅

Just to confirm, before posting this I used npm run test -- --coverage and it also works.

// jest.config.js

const path = require('path')

const ignores = [
  '/node_modules/',
  '/fixtures/',
  '/__tests__/helpers/',
  '__mocks__',
]

module.exports = {
  roots: [path.join(__dirname, 'src')],
  testEnvironment: 'node',
  testURL: 'http://localhost',
  moduleFileExtensions: ['js', 'jsx', 'json', 'ts', 'tsx'],
  collectCoverageFrom: ['src/**/*.+(js|jsx|ts|tsx)'],
  testMatch: ['**/__tests__/**/*.+(js|jsx|ts|tsx)'],
  testPathIgnorePatterns: [...ignores],
  coveragePathIgnorePatterns: [...ignores, 'src/(umd|cjs|esm)-entry.js$'],
  transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
  coverageThreshold: {
    global: {
      branches: 100,
      functions: 100,
      lines: 100,
      statements: 100,
    },
  },
}

Should I do anything else, or is this good to merge?

Also, as a side note note related to this PR directly, you might want to:

  1. Update the Add yourself as a contributor section in CONTRIBUTING.md since kcd-scripts doesn't have the contributors script anymore
  2. Either update the opt into git hooks section in CONTRIBUTING.md or actually make them optional, as they are auto-installed on setup, which means they are mandatory right now
kentcdodds commented 4 years ago

@all-contributors please add @futagoza for code and tests

allcontributors[bot] commented 4 years ago

@kentcdodds

I've put up a pull request to add @futagoza! :tada:

kentcdodds commented 4 years ago

:tada: This PR is included in version 2.7.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

futagoza commented 4 years ago

I forgot to update the user docs 😅, should I refork and update it, or you gonna do that later yourself?

kentcdodds commented 4 years ago

If you could make another pull request that would be great. Thanks!