DEV Community

shunku
shunku

Posted on

Chapter 10: Building a Project from Scratch

Planning and Setting Up Your Project

To start a project, you'll need to initialize a new project directory, and install any necessary dependencies. Depending on the framework, the setup process may differ:

Next.js

Start a new Next.js project:

npx create-next-app@latest my-app
cd my-app
Enter fullscreen mode Exit fullscreen mode

Remix

Start a new Remix project:

npx create-remix@latest my-app
# follow the prompts to configure your Remix app
cd my-app
Enter fullscreen mode Exit fullscreen mode

Gatsby

Start a new Gatsby site:

npm install -g gatsby-cli
gatsby new my-app
cd my-app
Enter fullscreen mode Exit fullscreen mode

Angular

Start a new Angular application:

npm install -g @angular/cli
ng new my-app
cd my-app
Enter fullscreen mode Exit fullscreen mode

Vue.js

Start a new Vue.js project:

npm install -g @vue/cli
vue create my-app
cd my-app
Enter fullscreen mode Exit fullscreen mode

Implementing Features

In your App component (or equivalent), display a "Hello, World!" message. Here's how you can do this in each framework:

Next.js

In pages/index.js:

function HomePage() {
  return <h1>Hello, World!</h1>
}

export default HomePage;
Enter fullscreen mode Exit fullscreen mode

Remix

In app/routes/_index.jsx:

export const meta = () => {
  return [
    { title: "New Remix App" },
    { name: "description", content: "Welcome to Remix!" },
  ];
};

export default function Index() {
  return (
    <div>
      <h1>Hello World!</h1>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Gatsby

In src/pages/index.js:

import React from "react"

const IndexPage = () => <h1>Hello, World!</h1>

export default IndexPage
Enter fullscreen mode Exit fullscreen mode

Angular

In src/app/app.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<h1>Hello, World!</h1>`,
})
export class AppComponent {}
Enter fullscreen mode Exit fullscreen mode

Vue.js

In src/App.vue:

<template>
  <h1>Hello, World!</h1>
</template>
Enter fullscreen mode Exit fullscreen mode

Debugging and Testing Your Project

For testing, Jest is commonly used in all frameworks for unit testing but Angular, which uses Jasmine. Cypress is used for end-to-end testing in all. Here is how to install Jest and Cypress, and a sample test for each:

Jest

Install Jest:

npm install --save-dev jest
Enter fullscreen mode Exit fullscreen mode

A simple Jest test (App.test.js) could look like this:

test('displays hello world', () => {
  const message = "Hello, World!";
  expect(message).toBe("Hello, World!");
});
Enter fullscreen mode Exit fullscreen mode

To run your Jest tests:

npx jest
Enter fullscreen mode Exit fullscreen mode

Cypress

Install Cypress:

npm install --save-dev cypress
Enter fullscreen mode Exit fullscreen mode

A simple Cypress test (cypress/integration/app_spec.js) might look like this:

describe('Homepage', () => {
  it('displays hello world', () => {
    cy.visit('/');
    cy.contains('h1', 'Hello, World!');
  });
});
Enter fullscreen mode Exit fullscreen mode

To run your Cypress tests:

npx cypress open
Enter fullscreen mode Exit fullscreen mode

Angular

For Angular, instead of Jest and Cypress, you'll use Jasmine and Cypress (or Nightwatch, WebdriverIO).

A simple Jasmine test (src/app/app.component.spec.ts) could look like:

it('should display hello world', () => {
  const fixture = TestBed.createComponent(AppComponent);
  fixture.detectChanges();
  const compiled = fixture.nativeElement;
  expect(compiled.querySelector('h1').textContent).toContain('Hello, World!');
});
Enter fullscreen mode Exit fullscreen mode

To run your Jasmine and Cypress tests:

ng test
ng e2e
Enter fullscreen mode Exit fullscreen mode

Deploying Your Project

Depending on the framework and the complexity of your project, there are numerous ways to deploy your application. For simpler applications, Vercel, Netlify, and GitHub Pages are popular choices.

Please follow the respective deployment guide for your chosen framework:

Remember to commit and push your changes to a remote repository before deploying your application.

Top comments (0)