stylelint / jest-preset-stylelint

Common configuration options for Jest in Stylelint environments.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Throws when used with latest stylelint

danez opened this issue · comments

Clearly describe the bug

When I use this preset as described in the README it throws an error.

What steps are needed to reproduce the bug?

jest.config.js

'use strict';

module.exports = {
    preset: 'jest-preset-stylelint',
};

What did you expect to happen?

Tests work

What actually happened (e.g. what warnings or errors did you get)?

  ● rg/existing-import-path › reject › [true,{}] › "@import url(theme);" › reject multiple imports

    TypeError: stylelint is not a function

      at Object.spec (node_modules/jest-preset-stylelint/jest-setup.js:84:24)

stylelint version: 13.2.0
jest version: 24.9.0
nodejs version 12.16.1

@danez Thanks for the report and for using the template.

I can reproduce this locally.

I'll create a pull request to bring this preset up to date as it has fallen behind the setup we use in stylelint itself to test the built-in rules.

Am I right in thinking you want to use this preset to test a stylelint plugin you're writing, and you want to use the testRule schema?

Am I right in thinking you want to use this preset to test a stylelint plugin you're writing, and you want to use the testRule schema?

Exactly, although I also run into a different issue now: The plugin I have is about imports, and checks if the import is correct. But if stylelint was called with stdin content instead of a file we cannoot really resolve the import, so we skip the rule:

        const source = root.source.input.file;
        if (!source) {
            return;
        }

Now I cannot test this afaics because i cannot mock any of this. I could add an option for testing that skips this check, but wanted to ask first if there is something better you could think of?

This preset may not be a good fit. It's mostly used to speed up writing countless (fixable) syntax tests, like so. As your plugin is concerned with resolving imports, I think you're best using the stylelint Node.js API directly so that you can use fixtures for your tests. You can use files option when testing using fixtures. The code option can be used to test your source conditional.

I've pulled out the bits from the testRule function that I think are relevant to your needs:

const { lint } = require("stylelint");

const config = {
  plugins: ["./lib/index.js"],
  rules: {
    "plugin/at-import-no-unresolveable": [true],
  },
};

it("warns for unresolveable import", async () => {
  const {
    results: [{ warnings, parseErrors }],
  } = await lint({
    files: "fixtures/contains-unresolveable-import.css",
    config,
  });

  expect(parseErrors).toHaveLength(0);
  expect(warnings).toHaveLength(1);

  const [{ line, column, text }] = warnings;

  expect(text).toBe(
    "Unexpected unresolveable import (plugin/at-import-no-unresolveable)"
  );
  expect(line).toBe(1);
  expect(column).toBe(1);
});

it("doesn't warn for resolveable imports", async () => {
  const {
    results: [{ warnings, parseErrors }],
  } = await lint({
    files: "fixtures/contains-resolveable-import.css",
    config,
  });
  expect(parseErrors).toHaveLength(0);
  expect(warnings).toHaveLength(0);
});

it("doesn't warn for fileless sources", async () => {
  const {
    results: [{ warnings, parseErrors }],
  } = await lint({
    code: "@import url(unknown.css)",
    config,
  });
  expect(parseErrors).toHaveLength(0);
  expect(warnings).toHaveLength(0);
});

..

It sounds like a great plugin, by the way.

Closed by #14, which fixes the TypeError. We hope to release a new version later this week once we sort out compatibility with the rules built into stylelint. master is good to go for plugins authors, though.