(Solved) Test Suite in Gatsby.js

Does anyone have an idea or know how to get the test suit working in a gatsby site. It works in my dev environment but doesn’t show up on my ‘live’ version. I should also say I’m using Netlify to host.

Hi

You might find this page from the Gatsby site useful:

(https://www.gatsbyjs.org/docs/custom-html/)

Some steps I used to get it to get it to add the tests to a dummy site deployed to Surge. Copy the file “.cache/default-html.js” to “src/html.js”. You can run in bash:

cp .cache/default-html.js src/html.js

Now open that file in your editor add the tests script tag just before closing body tag eg:

<body>
.
.
.
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
</body>

Save then run gatsby build in the project folder.

You can check if it worked by running gastby serve and viewing on localhost:9000

I also checked it worked with a basic site deployed to Surge:

http://common-snake.surge.sh/contact/

Hope that helps :slight_smile:

I just tried this but no luck. Did that work for you when you ran Gatsby build?

Ok I just figured this out. I guess Netlify works differently compared to Surge. I’ve never used Surge myself.

Once you have selected the site/app from your Netlify control panel you’ll want to go:

Settings > Build and Deploy > Post Processing and there just copy pasta the script and give it whatever name you’d like.

I didn’t even have to deploy the site for it to start working.

Hi, yeah it worked fine for me.

Following the steps above you should see the script is added just before the closing body tag in the yourprojectname\public\index.html as it’s inserted there in the build step I believe.

Thinking about it, I suppose you could skip this and add the script direct to that same file, yourprojectname\public\index.html, after running gatsby build.

That means it should work no matter where you run/deploy it?

Just to be sure it wasn’t an issue/difference with Netlify, I dragged the same public folder of that simple site to Netlify and it worked straight away as expected, you can see it here:

https://gifted-lalande-2b99da.netlify.com/

Anyway, I’m glad you resolved it! :slight_smile:

I guess I should have checked to see that it was in fact included in my public folder. I think that may have been the issue in my case.

Thanks it always comes in handy to do something more than one way!