"redeclaration of import beforeEach" error in Robot Name

When I run the tests for the Robot Name exercise in Javascript, they fail with the message “redeclaration of import beforeEach”.

However, I can’t find anywhere where beforeEach is redeclared or reimported in the tests or my (not yet complete) implementation.

Could someone help me make sense of this?

Hi there @SneakyMallard,

That’s … weird.

Can we take a look at your code for this exercise (in a code block plz)? That might help us get to the bottom of it. :slightly_smiling_face:

I’m just a newbie trying to understand the code, okay?

But if I understood things correctly, there’s a beforeEach defined on a TEST_HELPER which is injected before the text of the tests and after any import

Right before this import, there’s code which deletes global imports by searching for a line which has import and from '@jest/globals simultaneously. But the test file for robot-name has import and from on different lines.

So, if I’m not completely wrong here (which is likely, really), the global imports here are not deleted and there’s a redeclaration of beforeEach

P.S.: had this same problem when doing this exercise two weeks ago on the web test runner, so did it locally. My code (which passes all the tests) shows the same error when I run it on the web test runner.

2 Likes

Thanks for that, @oxe-b! Didn’t have that context, so wanted to see the OPs code. But this sounds like it’s something else, maybe?

1 Like

Might be, it’s just a guess from a complete newbie (me, of course :sweat_smile:).

But my code shows the same error, so in the case it helps finding a solution, here it is:

export class Robot {
  #name;
  static #nameSet = new Set();

  constructor() {
    this.reset();
  }

  #randomChar(min, max) {
    const minCode = min.charCodeAt(0);
    const maxCode = max.charCodeAt(0);
    const randomCode = minCode + Math.random() * (1 + maxCode - minCode);
    return String.fromCharCode(randomCode);
  }

  get #digit() {
    return this.#randomChar("0", "9");
  }

  get #letter() {
    return this.#randomChar("A", "Z");
  }

  reset() {
    do {
      this.#name = this.#letter + this.#letter + this.#digit + this.#digit + this.#digit;
    } while (Robot.#nameSet.has(this.#name));
    Robot.#nameSet.add(this.#name);
  }

  get name() {
    return this.#name.slice(0);
  }

  static releaseNames() {
    Robot.#nameSet.clear();
  }
}

Yes, the error must be with the tests + something running online;
empty solutions and the skeleton file also fail with the same error online, but my solution passes locally (using node in the terminal, I don’t have the CLI installed).

Maybe a possible fix could be collapsing the import to one line?
I don’t know enough about Exercism’s infrastructure to know whether that might cause some formatting error though.

or maybe do a replace(/import.*from ['"]@jest\/globals['"];?/s, "") before splitting the text into lines? Not sure.

Maybe.
I don’t know enough about how Exercism works to be sure either.

Maybe one of the track maintainers will have a solution?

1 Like

This may be @SleeplessByte as he’s the one dealing with the web test runner, but I believe he’s onto it (coming from a different issue tho). Still, wouldn’t hurt to ping.

In the meantime, @oxe-b spot on debugging! Removing the global import from the tests file would likely solve the web test runner issue, but then the local test runner wouldn’t run… Also, you can’t edit the files before submitting.

@SneakyMallard The proper temporary solution would be to test and submit using the CLI. If the solution passes locally there shouldn’t be any problem with that workflow.

2 Likes

I guess one good thing with this issue is that I finally got around to installing the CLI :grinning:.

2 Likes

Y’all are correct. The replace in the web test runner is not correctly replacing over multiple lines. I knew it was going to be an issue eventually but :grin::grin::grin:.

Thanks for reporting!

@oxe-b replacing before splitting would be a solution but I really want to be able to see we commented it out, so the correct solution is to add /* before and */ after instead of // before.

Do you feel confident enough to try to PR it? The workflow to debug / test it is:

  1. Make a change
  2. Build
  3. Copy the imported.mjs and worker files from output to sample
  4. Run npx serve sample
  5. Test it manually.
3 Likes

Not exactly sure why people seem to fear the CLI. It certainly has its strengths.

3 Likes

@SleeplessByte

I’ll try. If I can’t make it work, I’ll warn here so another can make the PR, ok?

@Cool-Katt

Very happy for being able to find the problem ^^

I’m reading exercism code to familiarize myself with professional JS.

1 Like

I’ve done the change, build, copy of files and serving. As for the testing, is it this fruit-picker.html? If so, the result is a pass with or without breaking the import into different lines.

Now what should I do?

Added a PR here

Please notice the lastIndexOf there. I’m not sure if that’s the best approach, but couldn’t come out with anything better at the time.

2 Likes

You want to add enters in the import in the test file ( no need to commit ). If it then still works, we’re good!

Did that and also created a robot-name.html (using fruit picker as model) with known bugged code. Everything passed on both cases.

1 Like

That’s perfect. I’ll include this in the release where I solve the other thing open and then you get rep again :wave:t4::clap:t4::ok_hand:t4::muscle:t4:

3 Likes

@SleeplessByte for some reason, it seems the problem remains.

It’s possible that it is not live yet. Quite a few changes in the queue and some of them not yet done!

2 Likes