Giter Site home page Giter Site logo

grafana / grafana-sdk-mocks Goto Github PK

View Code? Open in Web Editor NEW
6.0 132.0 18.0 33 KB

Mocks package for the Grafana SDK to be used when developing TypeScript plugins for Grafana. Includes typings so that the plugin can be built and mocks for the Grafana SDK so that Karma tests will work.

License: MIT License

TypeScript 18.09% JavaScript 81.91%

grafana-sdk-mocks's Introduction

NOTE, this is no longer the recommended way to develop Grafana Plugins:

We recommend copying one of the following projects and using its config files:


Grafana SDK Mocks for Plugins

This package facilitates writing Grafana plugins in TypeScript.

  • provides Typescript typings for the most common Grafana classes.
  • provides some simple fakes and util files to test plugins with Karma

Setup

With npm, Grunt, karma and mocha.js

This shows how to setup a project with Grunt as the build system, using npm to install the packages (yarn is very similar), karma for unit testing and mocha with expect.js for unit testing.

  1. npm install --save systemjs lodash moment (lodash and moment are optional)
  2. npm install --save-dev grunt grunt-contrib-clean grunt-contrib-copy grunt-contrib-watch grunt-typescript karma karma-expect karma-mocha karma-chrome-launcher karma-sinon karma-systemjs karma-phantomjs-launcher plugin-typescript q sinon
  3. Install this package: npm install --save-dev grafana/grafana-sdk-mocks
  4. Here is an example Gruntfile for building TypeScript. It expects the TypeScript files to be located in a src subdirectory.
module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);

  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-typescript');
  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.initConfig({
    clean: ['dist'],

    copy: {
      dist_js: {
        expand: true,
        cwd: 'src',
        src: ['**/*.ts', '**/*.d.ts'],
        dest: 'dist'
      },
      dist_html: {
        expand: true,
        flatten: true,
        cwd: 'src/partials',
        src: ['*.html'],
        dest: 'dist/partials/'
      },
      dist_statics: {
        expand: true,
        flatten: true,
        src: ['src/plugin.json', 'LICENSE', 'README.md'],
        dest: 'dist/'
      }
    },

    typescript: {
      build: {
        src: ['dist/**/*.ts', '!**/*.d.ts'],
        dest: 'dist',
        options: {
          module: 'system',
          target: 'es5',
          rootDir: 'dist/',
          declaration: true,
          emitDecoratorMetadata: true,
          experimentalDecorators: true,
          sourceMap: true,
          noImplicitAny: false,
        }
      }
    },

    watch: {
      files: ['src/**/*.ts', 'src/**/*.html', 'src/plugin.json', 'README.md'],
      tasks: ['default'],
      options: {
        debounceDelay: 250,
      },
    }
  });

  grunt.registerTask('default', [
    'clean',
    'copy:dist_js',
    'typescript:build',
    'copy:dist_html',
    'copy:dist_statics'
  ]);
};
  1. Here is an example karma.conf.js file. It assumes that test files are located in the specs subdirectory. It includes config for lodash, momentjs and q (promises).
'use strict';
module.exports = function(config) {
    config.set({
      frameworks: ['systemjs', 'mocha', 'expect', 'sinon'],

      files: [
        'specs/*.ts',
        { pattern: 'src/**/*.ts', included: false },
        { pattern: 'node_modules/grafana-sdk-mocks/**/*.ts', included: false },
        { pattern: 'node_modules/grafana-sdk-mocks/**/*.js', included: false },
        { pattern: 'node_modules/typescript/lib/typescript.js', included: false },
        { pattern: 'node_modules/lodash/lodash.js', included: false },
        { pattern: 'node_modules/moment/moment.js', included: false },
        { pattern: 'node_modules/q/q.js', included: false },
      ],

      systemjs: {
      //   // SystemJS configuration specifically for tests, added after your config file.
      //   // Good for adding test libraries and mock modules
        config: {
          // Set path for third-party libraries as modules
          paths: {
            'systemjs': 'node_modules/systemjs/dist/system.js',
            'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js',
            'lodash': 'node_modules/lodash/lodash.js',
            'moment': 'node_modules/moment/moment.js',
            'q': 'node_modules/q/q.js',
            'typescript': 'node_modules/typescript/lib/typescript.js',
            'plugin-typescript': 'node_modules/plugin-typescript/lib/plugin.js',
            'app/': 'node_modules/grafana-sdk-mocks/app/',
          },

          map: {
              'plugin-typescript': 'node_modules/plugin-typescript/lib/',
              'typescript': 'node_modules/typescript/',
              'app/core/utils/kbn': 'node_modules/grafana-sdk-mocks/app/core/utils/kbn.js'
          },

          packages: {
            'plugin-typescript': {
                'main': 'plugin.js'
            },
            'typescript': {
                'main': 'lib/typescript.js',
                'meta': {
                    'lib/typescript.js': {
                        'exports': 'ts'
                    }
                }
            },
            'app': {
              'defaultExtension': 'ts',
              'meta': {
                '*.js': {
                  'loader': 'typescript'
                }
              }
            },
            'src': {
              'defaultExtension': 'ts',
            },
            'specs': {
              'defaultExtension': 'ts',
              'meta': {
                '*.js': {
                  'loader': 'typescript'
                }
              }
            },
          },

          transpiler: 'plugin-typescript',
        }
      },

      reporters: ['dots'],

      logLevel: config.LOG_INFO,

      browsers: ['PhantomJS']
    });
};

Typings

Use the following triple slash directive to use Grafana classes in your code. The directive will point the TypeScript compiler at the mocks package so that it can find the files it needs to build. Place the directive at the top of all your TypeScript files:

///<reference path="../node_modules/grafana-sdk-mocks/app/headers/common.d.ts" />

Commands to build and test

  • grunt to build. This will create the dist subdirectory, copy the TypeScript files there, transpile them to JavaScript as well as copying html files, the License file, the Readme and plugin.json to dist.
  • grunt watch runs grunt when a file is changed. Useful while developing.
  • karma start --single-run runs the unit tests in the specs subdirectory.
  • karma start is a test watcher for unit tests. It will rerun the tests when a file is changed.

grafana-sdk-mocks's People

Contributors

daniellee avatar jtlisi avatar ryantxu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grafana-sdk-mocks's Issues

Can this package be used when I unit test my panel plugin using mocha?

Hi,
I'm working on unit testing my panel plugin now. The first trouble I encountered was
module not found:
Cannot find module 'grafana/app/plugins/sdk'
Seems grafana was not locally installed in node_modules, so how should I deal with it?
May I use your mock package, like put it into my src dir?
Thanks!

es6-shim is out of date, conflicts with lodash

The version of the es6-shim typing bundled with the grafana mocks is missing a breaking change, DefinitelyTyped/DefinitelyTyped#15588. The result is that if a plugin uses these mocks and lodash (and many, including grafana/typescript-template-datasource do just that) there are type declaration conflicts related to WeakMap.

As an aside, updating to the very latest version will also fix a PropertyKey duplicate identifier error related to a different PR.

MetricsPanelCtrl incompletely declared in common.d.ts

I have effectively no prior experience with TypeScript (or JavaScript for that matter), so feel free to disregard this if it's ridiculous.

I'm looking at a panel developed internally at my organization that was originally copied from a builtin Grafana panel. I'm working on converting it to a proper plugin.

The controller for this panel extends MetricsPanelCtrl, which it imports from app/plugins/sdk. When built in the Grafana source tree, as it currently stands, this import ultimately resolves to the real MetricsPanelCtrl. As a plugin, using grafana-sdk-mocks with a reference path directive to include app/headers/common.d.ts, it resolves to this empty definition:

export class MetricsPanelCtrl{}

I therefore get a bunch of errors from the TypeScript compiler related to fields and methods not being found.

Should the definition linked above perhaps be fleshed out to include declarations to extend PanelCtrl, and to contain the fields and methods that the real MetricsPanelCtrl contains?

Missing panel constructor definitions in common.d.ts

I am new to this, so I could be all wrong ๐Ÿ˜„

I am trying to convert influx-admin-panel to typescript. In the call to super() in my constructor

I got the error:

error TS2346: Supplied parameters do not match any signature of call target.

Everything works when I add the following line to common.d.ts:

declare module 'app/plugins/sdk' {
  export class PanelCtrl{
   ...
    constructor($scope: any, $injector: any);   <<< Add this line

Am I doing something wrong? or is the config missing constructor params?

Sdk out of sync with grafana

I like this sdk-mock idea so I don't have to checkout the whole Grafana tree to develop plugins.
Unfortunately it's out of sync with Grafana main source tree. For example the PanelCtrl class misses $location property.

Is this sdk-mock official way to develop Grafana plugins?

The developer guid has no mentioning of this node module. However the typescript template datasource example linked from the guide uses it.

The sdk-mock has no version tags. Looks like an experimental project to me.

Created a PR #10 that patches the sdk just to pass my compiling. I'm sure there are many more missing. Can the stubs be autogenerated?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.