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

chore(netlify): use Node.js for testing #158

Merged

Conversation

abidjappie
Copy link
Contributor

@abidjappie abidjappie commented Feb 7, 2024

Changes

Challenges

Testing

For now, run the following command in packages/netlify

node --test **/*.test.js

Docs

For now there is no documentation, but that might change

Copy link

changeset-bot bot commented Feb 7, 2024

⚠️ No Changeset found

Latest commit: 260fa6f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@abidjappie abidjappie marked this pull request as draft February 7, 2024 00:24
@alexanderniebuhr
Copy link
Member

@abidjappie Thanks for the PR. Maybe you can consult with @Skn0tt to see if we can rewrite the snapshot test to be still valuable without a snapshot :)

@abidjappie
Copy link
Contributor Author

Something I noticed is that either Image CDN or Middleware tests will fail when running on local (when one passes the other fails). I suspect it's related to how it builds the fixtures... in either case, it cannot find the file.

Image CDN:

[Error: ENOENT: no such file or directory, open '/Users/abid.jappie/code/withastro-adapters/packages/netlify/test/functions/fixtures/middleware/dist/_astro/astronaut.jcSXTCiO_1Hy9L1.webp'] { errno: -2, code: 'ENOENT', syscall: 'open', path: '/Users/abid.jappie/code/withastro-adapters/packages/netlify/test/functions/fixtures/middleware/dist/_astro/astronaut.jcSXTCiO_1Hy9L1.webp' }

Middleware:

 [Error: ENOENT: no such file or directory, open '/Users/abid.jappie/code/withastro-adapters/packages/netlify/test/functions/fixtures/middleware/dist/_astro/astronaut.jcSXTCiO_1Hy9L1.webp'] { errno: -2, code: 'ENOENT', syscall: 'open', path: '/Users/abid.jappie/code/withastro-adapters/packages/netlify/test/functions/fixtures/middleware/dist/_astro/astronaut.jcSXTCiO_1Hy9L1.webp' }

Copy link
Contributor

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

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

looks good on first sight!

@Skn0tt
Copy link
Contributor

Skn0tt commented Feb 9, 2024

it cannot find the file.

Hmm, I wonder wether that also happens on CI 🤔 Mind sharing the full stacktraces, to see where the fs.open call originates?

@alexanderniebuhr
Copy link
Member

I'll also migrate this script https://github.com/withastro/astro/blob/main/scripts/cmd/test.js over to the adapters repo, because using the Node's cli has some downsides. Once I've migrated it, we can use it for this PR too.

@alexanderniebuhr
Copy link
Member

@abidjappie, @Skn0tt What is the state? Do we need still to figure stuff out, or can we update this PR with latest main and get this merged?

I also just merged #150 which adds support for astro-script test

@alexanderniebuhr alexanderniebuhr changed the title chore(@astro/netlify): use Node.js for testing chore(netlify): use Node.js for testing Feb 11, 2024
@abidjappie
Copy link
Contributor Author

@alexanderniebuhr I'll look into the feedback sometime today, and try to get some stack traces.
After that I would like to run the CI tests so we can see if it errors out or not before proceeding 🙇

@abidjappie
Copy link
Contributor Author

@Skn0tt

Hmm, I wonder wether that also happens on CI 🤔 Mind sharing the full stacktraces, to see where the fs.open call originates?

I'm running the tests via node --test. In my attempt to find more information I surrounded both the test case, and the before each with a catch... and it seems to be occurring in the beforeEach level. Specifically:

await fixture.build();

My guess is that the test are running concurrently, which is why it fails in different places each time. I wrapped it in console.trace:

Trace: Error: ENOENT: no such file or directory, stat '/Users/abid.jappie/code/withastro-adapters/packages/netlify/test/functions/fixtures/middleware/.netlify/functions-internal/ssr/manifest_n9sb-Uc7.mjs'
    at Object.statSync (node:fs:1690:3)
    at removeEmptyDirs (file:///Users/abid.jappie/code/withastro-adapters/node_modules/.pnpm/astro@4.3.5_@types+node@18.19.2_typescript@5.3.2/node_modules/astro/dist/core/fs/index.js:12:11)
    at file:///Users/abid.jappie/code/withastro-adapters/node_modules/.pnpm/astro@4.3.5_@types+node@18.19.2_typescript@5.3.2/node_modules/astro/dist/core/fs/index.js:18:7
    at Array.map (<anonymous>)
    at removeEmptyDirs (file:///Users/abid.jappie/code/withastro-adapters/node_modules/.pnpm/astro@4.3.5_@types+node@18.19.2_typescript@5.3.2/node_modules/astro/dist/core/fs/index.js:16:11)
    at cleanStaticOutput (file:///Users/abid.jappie/code/withastro-adapters/node_modules/.pnpm/astro@4.3.5_@types+node@18.19.2_typescript@5.3.2/node_modules/astro/dist/core/build/static-build.js:314:5)
    at async staticBuild (file:///Users/abid.jappie/code/withastro-adapters/node_modules/.pnpm/astro@4.3.5_@types+node@18.19.2_typescript@5.3.2/node_modules/astro/dist/core/build/static-build.js:104:7)
    at async AstroBuilder.build (file:///Users/abid.jappie/code/withastro-adapters/node_modules/.pnpm/astro@4.3.5_@types+node@18.19.2_typescript@5.3.2/node_modules/astro/dist/core/build/index.js:138:5)
    at async AstroBuilder.run (file:///Users/abid.jappie/code/withastro-adapters/node_modules/.pnpm/astro@4.3.5_@types+node@18.19.2_typescript@5.3.2/node_modules/astro/dist/core/build/index.js:169:7)
    at async build (file:///Users/abid.jappie/code/withastro-adapters/node_modules/.pnpm/astro@4.3.5_@types+node@18.19.2_typescript@5.3.2/node_modules/astro/dist/core/build/index.js:47:3) {
  errno: -2,
  syscall: 'stat',
  code: 'ENOENT',
  path: '/Users/abid.jappie/code/withastro-adapters/packages/netlify/test/functions/fixtures/middleware/.netlify/functions-internal/ssr/manifest_n9sb-Uc7.mjs'
}
    at TestContext.<anonymous> (file:///Users/abid.jappie/code/withastro-adapters/packages/netlify/test/functions/edge-middleware.test.js:18:13)
    at async TestHook.run (node:internal/test_runner/test:574:9)
    at async node:internal/test_runner/test:509:9
    at async Suite.runHook (node:internal/test_runner/test:507:7)
    at async Test.run (node:internal/test_runner/test:549:9)
    at async Suite.processPendingSubtests (node:internal/test_runner/test:318:7)

@Skn0tt
Copy link
Contributor

Skn0tt commented Feb 13, 2024

Could be because of concurrency, yes! I see you replaced before with beforeEach in the edge-middleware test, what happens when you switch that back to before?

@abidjappie
Copy link
Contributor Author

@alexanderniebuhr can you help me to get the test running using astro-scripts.
I pulled latest branch and tried moving to it, but I get no output:

> @astrojs/netlify@5.1.1 test /Users/abid.jappie/code/withastro-adapters/packages/netlify
> pnpm run test-fn && pnpm run test-static

> @astrojs/netlify@5.1.1 test-fn /Users/abid.jappie/code/withastro-adapters/packages/netlify
> astro-scripts --test ./test/functions/*.test.js

> @astrojs/netlify@5.1.1 test-static /Users/abid.jappie/code/withastro-adapters/packages/netlify
> astro-scripts --test ./test/static/*.test.js

node --test **/*.test.js works fine.

@abidjappie
Copy link
Contributor Author

abidjappie commented Feb 13, 2024

@Skn0tt
I managed to get edge-middleware.test.js to pass, there are two parts:

  1. You are totally right that changing before to beforeEach was part of the solution.
  2. We also need to run the await fixture.clean() in the after

Unfortunately, I think this might solve the issue of file access overlapping in a single test, I think that different test files can still have this effect on one another: edge-middleware now consistently passes, but image-cdn consistently fails.

I can easily break edge-middleware by adding await fixture.clean() inside of image-cdn.test.js.
Is there someway to add a test specific config to test-utils.js or can we somehow isolate the tests/ ensure they don't run in parallel?

This also works for running image-cdn.test.js in isolation: await fixture.clean() after each it:

node --test **/image-cdn.test.js
▶ Image CDN
  ▶ when running outside of netlify
    ✔ does not enable Image CDN (714.118083ms)
  ▶ when running outside of netlify (714.634083ms)

  ▶ when running inside of netlify
    ✔ enables Netlify Image CDN (326.024042ms)
    ✔ respects image CDN opt-out (434.420417ms)
  ▶ when running inside of netlify (760.604291ms)

▶ Image CDN (1475.79075ms)

ℹ tests 3
ℹ suites 3
ℹ pass 3
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 2056.534583

@Skn0tt
Copy link
Contributor

Skn0tt commented Feb 13, 2024

Hmm, I'm not sure I follow. Could you push those changes? then I can try running it locally to see what's going wrong.

@alexanderniebuhr
Copy link
Member

alexanderniebuhr commented Feb 13, 2024

@abidjappie let me check astro-scripts

@abidjappie
Copy link
Contributor Author

@Skn0tt

Hmm, I'm not sure I follow. Could you push those changes? then I can try running it locally to see what's going wrong.

You can try with this branch. State: edge-middleware should pass, image-cdn seems to fail.
f26386e

If you surround each case in image-cdn with test.only you can of course get it to pass.

@alexanderniebuhr
Copy link
Member

This is interesting, using astro-scripts test does not run tests in parallel. See: https://github.com/withastro/adapters/blob/main/scripts/cmd/test.js#L42-L54

@abidjappie
Copy link
Contributor Author

abidjappie commented Feb 14, 2024

@alexanderniebuhr true. I also noticed that running with astro-scripts test ./test/functions/*.test.js only runs the first test in the folder.

pnpm run test-fn

> @astrojs/netlify@5.1.2 test-fn /Users/abid.jappie/code/withastro-adapters/packages/netlify
> astro-scripts test ./test/functions/*.test.js

▶ Cookies
  ✔ Can set multiple (13.354375ms)
  ✔ renders dynamic 404 page (4.59525ms)
▶ Cookies (597.759083ms)

ℹ tests 2
ℹ suites 1
ℹ pass 2
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 1190.360458

I'm not sure if it supports recursive, but this works for me:

    "test-fn": "for f in test/functions/*.test.js; do echo $f; astro-scripts test $f; done"

Should we update the script ?

@abidjappie
Copy link
Contributor Author

abidjappie commented Feb 14, 2024

@Skn0tt I've pushed some changes now. You can get all tests to pass.

for f in test/functions/*.test.js; do echo $f; pnpm astro-scripts test $f; done

It may be related to extraneous asynchronous activity since removing the env cleanup in afa37ea somehow fixed it.

I think it may need further investigation at some point, but I'm just relieved it's behaving somewhat consistently on my machine now.

Copy link
Contributor

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

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

Looks wonderful! Good job at getting all the tests to pass under a new test runner :D

Left a tiny nit, but nothing blocking.

@alexanderniebuhr
Copy link
Member

alexanderniebuhr commented Feb 14, 2024

@abidjappie Thank you so much for your contribution. Appreciate the help ❤️ I'll take over from here and make sure this get's merged asap.

@alexanderniebuhr alexanderniebuhr marked this pull request as ready for review February 14, 2024 18:58
@alexanderniebuhr alexanderniebuhr merged commit f52bf88 into withastro:main Feb 16, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants