Setting Up Testing Environment
Configure a testing environment using tools like Jest or QUnit, and set up assertions to verify the behavior of your jQuery functions and plugins.
Setting Up Testing Environment is a free jQuery Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the jQuery Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Test Your jQuery Code?
Writing tests for your jQuery code is crucial for building robust and maintainable web applications. It helps catch bugs early and ensures your code behaves as expected.
- Prevents Regressions: Ensures new changes don't break existing functionality.
- Improves Code Quality: Forces you to write modular, testable code.
- Facilitates Collaboration: Provides clear documentation of how components should work.
Choosing a Test Runner
For testing JavaScript, including jQuery, you need a test runner. This tool executes your tests and reports the results.
Two popular options are:
- Jest: A modern, feature-rich testing framework from Facebook, widely used for React but great for any JS.
- QUnit: A jQuery-specific testing framework, simpler but less feature-rich than Jest.
In this lesson, we'll focus on setting up Jest due to its comprehensive features and broad adoption.
Initialize Your Project (Node.js)
Before installing Jest, you need a Node.js project. If you don't have one, you can easily create it.
Navigate to your project directory in the terminal and run:
npm init -yThis command creates a package.json file, which manages your project's dependencies and scripts.
Install Jest & JSDOM
Now, let's install Jest. Since jQuery often interacts with the browser's DOM (Document Object Model), we'll also need jest-environment-jsdom. JSDOM provides a browser-like environment in Node.js, so your jQuery code can manipulate a virtual DOM.
Install them as development dependencies:
npm install --save-dev jest jest-environment-jsdomThis adds Jest and JSDOM to your package.json.
Configure Jest for JSDOM
To tell Jest to use JSDOM, you need to configure it. You can do this by adding a test script to your package.json or by creating a jest.config.js file.
Option 1: In package.json
{
"name": "my-jquery-app",
"version": "1.0.0",
"scripts": {
"test": "jest --env=jsdom"
},
"devDependencies": {
"jest": "^29.x.x",
"jest-environment-jsdom": "^29.x.x"
}
}The --env=jsdom flag ensures Jest runs tests in a JSDOM environment.
Your First Test File
Jest looks for test files in a __tests__ directory or files ending with .test.js (or .spec.js). Let's create a simple test file.
Create a directory named __tests__ in your project root, then add a file like __tests__/my-feature.test.js:
// __tests__/my-feature.test.js
describe('My Feature', () => {
test('should do something basic', () => {
// Test logic will go here
});
});describe groups related tests, and test (or it) defines an individual test case.
Importing jQuery for Tests
To test jQuery code, you need to import jQuery itself into your test file. You can install jQuery via npm: npm install jquery.
Then, in your test file, you can import it:
// __tests__/my-feature.test.js
const $ = require('jquery');
describe('My jQuery Feature', () => {
// Setup for each test
beforeEach(() => {
// Create a fresh DOM element for each test
document.body.innerHTML = 'Hello';
});
test('should add a class to the element', () => {
const element = $('#test-element');
element.addClass('highlight');
expect(element.hasClass('highlight')).toBe(true);
});
});Writing Basic Assertions
Assertions are how you verify that your code behaves correctly. Jest uses the expect() function combined with matchers to make assertions.
Here's a simple JavaScript example (not jQuery specific, but demonstrates assertions):
function addNumbers(a, b) {
return a + b;
}
// Example of how Jest would test this:
// expect(addNumbers(2, 3)).toBe(5);
// expect(addNumbers(-1, 1)).toBe(0);
console.log("2 + 3 = " + addNumbers(2, 3));
console.log("5 + 10 = " + addNumbers(5, 10));Common Jest Matchers
Jest offers a rich set of matchers. Here are a few common ones you'll use:
.toBe(value): Checks for exact equality (like===)..toEqual(value): Checks for deep equality of objects or arrays..not.toBe(value): Checks for inequality..toBeTruthy()/.toBeFalsy(): Checks for truthiness/falsiness..toContain(item): Checks if an array or string contains an item.
For jQuery, you'll often combine these with jQuery methods, like expect($element.text()).toBe('Expected Text');
Running Your Tests
Once your test files are set up, running them is simple. Just use the script we defined in package.json:
npm testJest will find all your test files, run them in the JSDOM environment, and report the results in your terminal. It will show you which tests passed, which failed, and provide details for any failures.
Quick Check on Setup
You've learned how to set up a testing environment for jQuery using Jest and JSDOM. Which of the following commands and configurations are essential for a basic Jest setup with DOM support?
Recap: Testing Environment Setup
In this lesson, you've learned how to set up a robust testing environment for your jQuery applications using Jest.
- We initialized a Node.js project with
npm init. - Installed Jest and
jest-environment-jsdomto simulate a browser environment. - Configured Jest in
package.jsonto use JSDOM. - Understood how to structure test files, import jQuery, and write basic assertions using Jest's matchers.
This foundation allows you to write effective tests, ensuring your jQuery code is reliable and maintainable.
Frequently asked questions
Is the “Setting Up Testing Environment” lesson free?
Yes — the full text of “Setting Up Testing Environment” is free to read here on the web, and the jQuery Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the jQuery Academy course, upgrade to CoddyKit PRO.
What will I learn in “Setting Up Testing Environment”?
Configure a testing environment using tools like Jest or QUnit, and set up assertions to verify the behavior of your jQuery functions and plugins. You practise jQuery Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start jQuery Academy?
No prior experience is required. jQuery Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Setting Up Testing Environment” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this jQuery Academy lesson?
Yes. Every jQuery Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Unit Testing jQuery
- Setting Up Testing Environment
- Writing Effective jQuery Tests