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

add tests for first class mixins #1933

Merged
merged 36 commits into from
Oct 5, 2023
Merged

Conversation

connorskees
Copy link
Contributor

@connorskees connorskees commented Aug 25, 2023

This is some initial work at adding the baseline tests for first class mixins.

Embedded node host PR: sass/embedded-host-node#253
Tracking issue: sass/sass#626
Protobuf changes: sass/sass#3674
dart-sass changes: sass/dart-sass#2073

[skip dart-sass]
[skip sass-embedded]

Closes #1927

@connorskees
Copy link
Contributor Author

It seems like, from some comments I read, that these tests are typically auto-generated? Is there documentation somewhere on how to generate these?

@nex3
Copy link
Contributor

nex3 commented Aug 30, 2023

The test cases are written by hand, but for error tests specifically the output can be auto-generated. Just write test cases with empty error output:

<===> test/input.scss
a {b: }

<===> test/error

then run npm run sass-spec --interactive. When it gets to one of these test cases, use O! to tell it to overwrite the output for all tests that have similar failures (in this case, "the error doesn't match the expected error output") to match the actual output generated by the test runner.

For bonus points, explicitly document this pattern in the README 🙂.

@connorskees connorskees marked this pull request as ready for review September 8, 2023 08:38
this test _shouldn't_ have a different error message as a result of the
mixin changes, but i am updating it now so that CI is green, before i
get a chance to investigate why this change is occurring.

i have verified that the change to this test is related to the changes
to mixins
previous failure seems spurious? at least, it says the error lies with
npm
@use "sass:meta";

@mixin foo() {
@content;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved. It would be nice if lint-spec enforced this or if lint-spec --fix fixed this -- it was somewhat tedious to grep for to find all places with the wrong indentation.

spec/core_functions/meta/accepts_content.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/accepts_content.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/apply.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/apply.hrx Show resolved Hide resolved
spec/core_functions/meta/get_mixin/scope.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/get_mixin/scope.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/module_mixins.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/module_mixins.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/module_mixins.hrx Show resolved Hide resolved
Copy link
Contributor

@nex3 nex3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there!

spec/core_functions/meta/accepts_content.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/accepts_content.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/apply.hrx Show resolved Hide resolved
spec/core_functions/meta/get_mixin/content.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/get_mixin/content.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/get_mixin/content.hrx Outdated Show resolved Hide resolved
spec/core_functions/meta/apply.hrx Show resolved Hide resolved
spec/core_functions/meta/get_mixin/content.hrx Outdated Show resolved Hide resolved
@ntkme
Copy link
Contributor

ntkme commented Sep 29, 2023

Can we add a js-api test can round-trip a mixin reference from Sass similar to this:

it('can round-trip a function reference from Sass', () => {
const fn = spy(args => {
expect(args).toBeArrayOfSize(1);
expect(args[0]).toBeInstanceOf(SassFunction);
return args[0];
});
expect(
compileString(
`
@use 'sass:meta';
@function plusOne($n) {@return $n + 1}
a {b: meta.call(foo(meta.get-function('plusOne')), 2)}
`,
{
functions: {'foo($arg)': fn},
}
).css
).toBe('a {\n b: 3;\n}');
expect(fn).toHaveBeenCalled();
});

@connorskees
Copy link
Contributor Author

Do we have JS API tests today that read a value from Sass? The function tests (and all the tests I've looked at so far) seem to involve declaring a function/another value in JavaScript and sending it to Sass, which is not possible with mixins.

@ntkme
Copy link
Contributor

ntkme commented Sep 29, 2023

Do we have JS API tests today that read a value from Sass? The function tests (and all the tests I've looked at so far) seem to involve declaring a function/another value in JavaScript and sending it to Sass, which is not possible with mixins.

The sass function test I linked above declares a sass function plusOne in sass code, pass it to the host via a host function foo and then the host function foo return the compiler function plusOne back to compiler, which then get invoked.

test ci with more prs linked in description
@ntkme
Copy link
Contributor

ntkme commented Sep 29, 2023

You should add [skip sass-embedded] to this PR's description, and then trigger test from dart-sass or embedded-host-node repo if needed.

@ntkme
Copy link
Contributor

ntkme commented Sep 29, 2023

Existing sass values has some test regarding assert like:

it("isn't any other type", () => {
expect(() => list.assertBoolean()).toThrow();
expect(() => list.assertCalculation()).toThrow();
expect(() => list.assertColor()).toThrow();
expect(() => list.assertFunction()).toThrow();
expect(() => list.assertMap()).toThrow();
expect(list.tryMap()).toBe(null);
expect(() => list.assertNumber()).toThrow();
expect(() => list.assertString()).toThrow();
});

it("isn't any other type", () => {
expect(value.assertCalculation).toThrow();
expect(value.assertColor).toThrow();
expect(value.assertFunction).toThrow();
expect(value.assertMap).toThrow();
expect(value.tryMap()).toBe(null);
expect(value.assertNumber).toThrow();
expect(value.assertString).toThrow();
});
});

We should add assertMixin to these tests.

this is not true in browser environments, for some reason
@nex3
Copy link
Contributor

nex3 commented Oct 4, 2023

What was wrong with the assertion in 55fe148?

@connorskees
Copy link
Contributor Author

I'm not sure -- I was somewhat hoping for a subsequent CI run to validate that the later checks are true to get a better idea.

This test failed in https://github.com/sass/dart-sass/actions/runs/6399530354/job/17372158557, only on the browser environment. I am expecting reproducing the run in the browser environment locally to be hard.

@connorskees
Copy link
Contributor Author

It seems to pass with the assertion removed, so it does correctly execute assertMixin. It just isn't an instanceof SassMixin in the browser? I'm not familiar enough with the end-to-end process here to know what would be causing this and why it only fails in the browser environment. My assumption at least is that it's not a bug.

@nex3
Copy link
Contributor

nex3 commented Oct 4, 2023

Every Sass value object is expected to be instanceof its type—this is part of the API contract we provide to the user, so if it's not passing somewhere that's a bug that needs to be fixed.

@nex3
Copy link
Contributor

nex3 commented Oct 4, 2023

We use isInstanceOf() everywhere else, why wouldn't it work here?

@connorskees
Copy link
Contributor Author

In this particular case, I changed the check from toBeInstanceOf to a bare instanceof check to get a better sense of the issue from the CI run. I would like for the check to eventually be toBeInstanceOf. I meant proper in the commit message in the sense of "instanceof proper".

I was hoping to be able to debug without having to download the chrome executable, but I suppose I will have to.

@nex3 nex3 merged commit c55ca91 into sass:main Oct 5, 2023
8 checks passed
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

Successfully merging this pull request may close these issues.

Add tests for first-class mixins
3 participants